c - Getting the address of a triple pointer to char -
gcc 4.4.4 c89
i wondering why getting different memory address. when print address of animals in main following:
animals [ rabbit ] : [ 0xbfab2e48 ] animals [ rabbit ] : [ 0xbfab2e48 ]
however, when print in function, different memory locations. think should same.
ptr animals [ rabbit ] : [ 0xbfab2e08 ] ptr animals [ rabbit ] : [ 0xbfab2e08 ]
many advice,
int main(void) { char *animals[] = {"rabbit", "cat", "dog", "elephant", "racoon", null}; char *countries[] = {"india", "amercia", "france", "spain", "canada", "mexico", null}; char *cars[] = {"ford fista", "masda 3", "honda city", "toyata cote", null}; char **ptr_data[] = {animals, countries, cars, null}; printf("animals [ %s ] : [ %p ]\n", *animals, (void*)animals); printf("animals [ %s ] : [ %p ]\n", animals[0], &animals[0]); print_data_ptr(ptr_data); return 0; } void print_data_ptr(char ***ptr) { char **data_list = null; printf("ptr animals [ %s ] : [ %p ]\n", *ptr[0], (void*)&ptr[0]); printf("ptr animals [ %s ] : [ %p ]\n", **ptr, (void*)ptr); }
animals
array of char *
values, , ptr_data
array of char **
values.
when initialise ptr_data
in line:
char **ptr_data[] = {animals, countries, cars, null};
animals
evaluated pointer first element - ptr_data[0]
same &animals[0]
- address of first char *
in animals
. same thing happens in 2 printf()
functions in main - animals
, &animals[0]
evaluate same thing, pointer value stored in ptr_data[0]
.
within function, ptr
pointer first element of ptr_data
in main - ptr
equivalent &ptr_data[0]
. &ptr[0]
equivalent ptr
- &ptr[0]
shows address of ptr_data[0]
, not stored there. if print ptr[0]
instead, address of animals[0]
.
Comments
Post a Comment