0
gcc 4.4.4 c89獲取三字指針字符的地址
我只是想知道爲什麼我得到不同的內存地址。當我在主打印動物的地址,我得到如下:
animals [ rabbit ] : [ 0xbfab2e48 ]
animals [ rabbit ] : [ 0xbfab2e48 ]
然而,當我在打印功能,我得到不同的存儲位置。我認爲他們應該是一樣的。
ptr animals [ rabbit ] : [ 0xbfab2e08 ]
ptr animals [ rabbit ] : [ 0xbfab2e08 ]
非常感謝任何建議,
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);
}