2010-09-01 221 views
1

gcc 4.4.3 c89獲取指針字符指針的地址字符指針

我想顯示地址。基本上,我只是想證明我正在顯示正確的地址。

我想指針顯示的每個陣列的地址爲char「device_gc」,「device_mg」,「device_cc」

所以我在我的主要功能顯示出來。但是,在我的display_list函數中,我只想證明我正在顯示正確的地址。輸出是一樣的。

我希望你明白嗎?

非常感謝您的任何建議。

#include <stdio.h> 

void display_list(char ***dev_list); 

int main(void) 
{ 
    char *device_gc[] = {"GCDEV01", "GCDEV02", "GCDEV03", "GCDEV04", "GCDEV05", "GCDEV06", NULL}; 
    char *device_mg[] = {"MGDEV01", "MGDEV02", "MGDEV03", "GCDEV05", NULL}; 
    char *device_cc[] = {"CCDEV01", "CCDEV02", "CCDEV03", "CCDEV04", "CCDEV05", NULL}; 

    char **device_list[] = {device_gc, device_mg, device_cc, NULL}; 

    printf("device_gc [ %p ]\n", (void*)*device_gc); 
    printf("device_mg [ %p ]\n", (void*)*device_mg); 
    printf("device_cc [ %p ]\n", (void*)*device_cc); 

    display_list(device_list); 

    return 0; 
} 

void display_list(char ***dev_list) 
{ 
    while(**dev_list != NULL) { 
     printf("dev [ %p ]\n", (void*)**dev_list++); 
    } 
} 

所需的輸出:

device_gc [ 0x80485e0 ] 
device_mg [ 0x8048610 ] 
device_cc [ 0x8048628 ] 
dev [ 0x80485e0 ] 
dev [ 0x8048610 ] 
dev [ 0x8048628 ] 

我得到實際的輸出是不同的,有時會導致核心轉儲。這是爲什麼?

+0

請寫明您的問題 – Sundar 2010-09-01 03:56:19

回答

2

兩個小調整。在打印main()中的值之前,您不應該取消引用'device_gc'等。你只應該在display_list()使用單一的間接引用:

#include <stdio.h> 

void display_list(char ***dev_list); 

int main(void) 
{ 
    char *device_gc[] = {"GCDEV01", "GCDEV02", "GCDEV03", "GCDEV04", "GCDEV05", "GCDEV06", NULL}; 
    char *device_mg[] = {"MGDEV01", "MGDEV02", "MGDEV03", "GCDEV05", NULL}; 
    char *device_cc[] = {"CCDEV01", "CCDEV02", "CCDEV03", "CCDEV04", "CCDEV05", NULL}; 

    char **device_list[] = {device_gc, device_mg, device_cc, NULL}; 

    printf("device_gc [ %p ]\n", (void*)device_gc); 
    printf("device_mg [ %p ]\n", (void*)device_mg); 
    printf("device_cc [ %p ]\n", (void*)device_cc); 

    display_list(device_list); 

    return 0; 
} 

void display_list(char ***dev_list) 
{ 
    while(*dev_list != NULL) { 
     printf("dev [ %p ]\n", (void*)*dev_list++); 
    } 
} 
+0

謝謝您的幫助。 – ant2009 2010-09-01 05:19:50