Herzlich Willkommen, lieber Gast!
  Sie befinden sich hier:

  Forum » C / C++ (ANSI-Standard) » Programm von Call by Value auf Call by Reference

Forum | Hilfe | Team | Links | Impressum | > Suche < | Mitglieder | Registrieren | Einloggen
  Quicklinks: MSDN-Online || STL || clib Reference Grundlagen || Literatur || E-Books || Zubehör || > F.A.Q. < || Downloads   

Autor Thread - Seiten: > 1 <
000
03.05.2008, 11:24 Uhr
~C-Lerner
Gast


Hallo,

ich belege einen Kurs zum C-Lernen.
Dort ist folgende Aufgabe gestellt worden:

Schreiben Sie das Programm von Aufgabe 3 von Call by Value auf Call by Reference um.

Call by Value (Kopie) und Call by Reference (Direkte Übergabe) ist mir klar. Aber ich habe leider keinen Ansatz, folgendes simples Beispiel auf Call by Reference umzuschreiben:


C++:
#include <stdio.h>
#include <stdlib.h>

typedef struct{
    int x;
    int y;
}Punkt;

struct Anker{ Punkt *ds; };

void speicher( struct Anker nxt ) {
    nxt.ds = malloc(sizeof(Punkt));
    if( nxt.ds != NULL) {
        printf("Erfolgreich\n");
        nxt.ds->x = 2;
        nxt.ds->y = 4;
        printf("x:%d\n",nxt.ds->x);
        printf("y:%d\n",nxt.ds->y);
    } else {
        printf("Fehler");
    }
}

int main() {
    struct Anker vAnker; vAnker.ds = NULL;
    speicher(vAnker);
    printf("Hauptprogramm:\n");
    if (vAnker.ds != NULL) {
        printf("x:%d\n",vAnker.ds->x);
        printf("y:%d\n",vAnker.ds->y);
    } else {
        printf("Keine Adresse\n");
    }
    return 0;
}


Hinter dem Programm ist nicht wirklicher Sinn, sondern nur der Beginn von Zeigern usw.

Vielen Dank für eure Hilfe!
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
001
03.05.2008, 14:18 Uhr
xXx
Devil


Denke jetzt einfach mal dass die das wollen:

C++:
void speicher(struct Anker* ptr_next)
{
    ptr_next->ds = malloc(sizeof(Punkt));
    if (ptr_next->ds == NULL) { printf("FEHLER!"); return; }

    printf("Erfolgreich\n");
    ptr_next->ds->x = 2;
    ptr_next->ds->y = 4;
    printf("x: %d\n", ptr_next->ds->x);
    printf("y: %d\n", ptr_next->ds->y);
}
...


C++:
speicher(&vAnker);
wäre dann der Aufruf ...
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
002
03.05.2008, 15:34 Uhr
~C-Lerner
Gast


Hallo,

sieht denke ich ganz gut aus.
Nur warum wird hier nicht mit ** "übernommen"?

Ist doch sonst auch, wenn man einen Zeiger übernimmt.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
003
04.05.2008, 15:48 Uhr
xXx
Devil


Weil du den Zeiger nicht umlenkst ... Wenn du bsw. folgendes machen willst:

C++:
void create_bar(bar** ptr_destination)
{
    *ptr_destination = new bar;
}
... dann brauchst u einen Zeiger-Zeiger
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
004
05.05.2008, 13:40 Uhr
0xdeadbeef
Gott
(Operator)


In C:

C++:
void create_bar(bar** ptr_destination)
{
    *ptr_destination = calloc(1, sizeof(bar));
}


In C++:

C++:
void create_bar(bar *&ptr_destination) {
  ptr_destination = new bar;
}


...in C++ ist es äußerst unschön, Referenzen mit Zeigern zu emulieren. Dafür gibt es Referenzen.
--
Einfachheit ist Voraussetzung für Zuverlässigkeit.
-- Edsger Wybe Dijkstra
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
005
05.05.2008, 14:30 Uhr
xXx
Devil


Jap Sry das new im Beispiel war unschön ... geht ja bei C nicht
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
Seiten: > 1 <     [ C / C++ (ANSI-Standard) ]  


ThWBoard 2.73 FloSoft-Edition
© by Paul Baecher & Felix Gonschorek (www.thwboard.de)

Anpassungen des Forums
© by Flo-Soft (www.flo-soft.de)

Sie sind Besucher: