cosztynel Posted June 24, 2011 Report Posted June 24, 2011 Salut.Avem codul:int a=5 , b=4;int *pa=&a, *pb=&b;void functie1(int &x,int &y){ //instructiuni......}void functie2(int *x, int *y){ //aceleasi instructiuni}Intrebare: Difera cu ceva functie1(a, fata de functie2(pa,pb) ?Exista vreo diferenta in a transmite argumentele prin referinta si prin pointeri catre ele? Quote
Zamolxis666 Posted June 25, 2011 Report Posted June 25, 2011 (edited) Da este o diferenta, cad apelezi functie1 in main o apelezi asa functie1(a, , iar pentru functie2: functie2((int *)a, (int *). In primul caz transmiti ca parametri adresele variabilelor, iar in cazul doi doar pointeri catre variabile si de aceea trebuie sa faci cast catre pointer la int daca presupunem ca variabilele a si b sunt declarate ca int a, b. Uite un exemplu mai jos:#include <stdio.h>#include <conio.h>void f1(int &x, int &y){ printf("%d %d\n", x, y);}void f2(int *x, int *y){ printf("%d %d\n", x, y);}int main(){ int x = 10, y = 11; f1(x, y); f2((int *)x, (int *)y); getch(); return 0;} Edited June 25, 2011 by Zamolxis666 Quote