tags:

views:

51

answers:

1

Hello,

gcc 4.4.3 c89

I am trying to display the address. Basically, I just want to prove that I am displaying the correct address.

I want to display the address of each array of pointers to char 'device_gc', 'device_mg', 'device_cc'

So I display them in my main function. However, in my display_list function I just want to prove I am displaying the correct address. The output is the same.

I hope you understand?

Many thanks for any suggestions.

#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++);
    }
}

Desired Output:

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

The actual output I am getting is different and sometimes leads to a core dump. Why is that?

+2  A: 

Two minor tweaks. You should not dereference 'device_gc' etc before printing the value in main(); you should only use a single dereference in 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++);
    }
}
Jonathan Leffler
Thanks, for your help.
robUK