c++ - Difference between Function pointer and passing pointers -
i have these codes...
#include <iostream> using namespace std; int * function (int a, int b); int main() { int = 2; int b = 7; int * x = &a; int * y = &b; cout << << " " << *x << endl; cout << b << " " << *y << endl; function (a , b); cout << endl << endl; cout << << " " << *x << endl; cout << b << " " << *y << endl; cout << endl << endl; cout << << " " << *x << endl; cout << b << " " << *y << endl; function (a , b); cout << endl << endl; cout << << " " << *x << endl; cout << b << " " << *y << endl; cout << endl << endl; cout << << " " << *x << endl; cout << b << " " << *y << endl; system ("pause"); return 0; } int * function (int a, int b) { int pom; pom = a; = b; b = pom; }
this 1 function pointer , not change variables. dont know why, , means fucntion pointer. why kind of pointers useful ?
versus
#include <iostream> using namespace std; int function (int * a, int * b); int main() { int = 2; int b = 7; int * x = &a; int * y = &b; cout << << " " << *x << endl; cout << b << " " << *y << endl; function (&a , &b); cout << endl << endl; cout << << " " << *x << endl; cout << b << " " << *y << endl; cout << endl << endl; cout << << " " << *x << endl; cout << b << " " << *y << endl; function (&a , & b); cout << endl << endl; cout << << " " << *x << endl; cout << b << " " << *y << endl; cout << endl << endl; cout << << " " << *x << endl; cout << b << " " << *y << endl; system ("pause"); return 0; } int function (int * a, int * b) { int pom; pom = *a; *a = *b; *b = pom; }
this ordinary passing pointers, change variables books learn. interested how use passing pointer expamle arrays.
you mixing concepts, none of funtion pointers
int * function (int a, int b); int function (int * a, int * b);
this:
int * function (int a, int b);
is function takes value 2 ints , returns pointer int
and this
int function (int * a, int * b);
is function takes 2 pointers ints , returns int
Comments
Post a Comment