c function change pointer value code example

Example 1: 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 2: 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);

Tags:

C Example