Jump to content
cosztynel

Si inca o nelamurire..

Recommended Posts

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,B) fata de functie2(pa,pb) ?:-/

Exista vreo diferenta in a transmite argumentele prin referinta si prin pointeri catre ele?

Link to comment
Share on other sites

Da este o diferenta, cad apelezi functie1 in main o apelezi asa functie1(a, B), iar pentru functie2: functie2((int *)a, (int *)B). 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 by Zamolxis666
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...