objective c - Address of a variable is changing when inside a block -


in following code, address of b changing when it's within block. why? , if changes b, why not a?

int b =42;     int  *a = &b;      printf("%p", a);     printf("%p", &b);        void (^testblock)(void) = ^(void)     {         printf("%p", a);  //address not changed         printf("%p", &b); //address changed      };     testblock();      printf("%p", a); //address not changed     printf("%p", &b);//address not changed 

a block similar function. consider:

void testfunc(int *a, int b) {     printf("%p", a);     printf("%p", &b); }  int b =42; int  *a = &b;  printf("%p", a); printf("%p", &b);  testfunc(a, b);  printf("%p", a); //address not changed printf("%p", &b);//address not changed 

the call function has copied values of both a , b local variables (parameters) in testfunc() happen named a , b. variables not same a , b in other scope. have same value. therefore, addresses of parameters different addresses of other variables.

in case of a, you're printing value, not address. so, that's same because value has been copied. in case of b, you're printing address.


Comments