c programm pointer change in function code example
Example 1: c programm pointer change in function
void change(int **p, int *someOtherAddress) // **p *b allazei o p kai deixnei sto b //
{
*p = someOtherAddress;
}
int a = 1, b = 2;
int *p = &a;
printf("*p = %d\n", *p);
change(&p, &b);
printf("*p = %d\n", *p);
Example 2: c programm pointer change in function
void foo(int** p) { //*p p den allazei , *p **p allazei! deikse null //
*p = NULL; /* set pointer to null */
}
void foo2(int* p) {
p = NULL; /* makes copy of p and copy is set to null*/
}
int main() {
int* k;
foo2(k); /* k unchanged */
foo(&k); /* NOW k == NULL */
}
Example 3: c programm pointer change in function
int* change_adrs(int *q) //me return kai anathesh: p=returned kai synarthsh me mesa *kati=&x x=5;//
{ //xwris reference, by value.Epistrefei apla dieythynsh kathorismenh sthn synarthsh panta to 875002//
int * otheraddess;
q = otheraddress;
return q;
}
int main()
{
p=change_adrs(p);
return 0;
}