tags:

views:

46

answers:

3

Hello,

gcc 4.4.3 c89

I have the following source file.

However, I get a stack dump on the following line:

printf("dest: %d [ %s ]\n", i, dest[i]);

The reason is that if fails to break from the for loop. I have tried different conditions in the for loop to try and break before printing.

i != NULL
i != '\0'
i != 0

However, all of the above fails to work. I could use the sizeof(src) as a condition. But I just want to know why I am getting this stack dump using my current method.

When I check using gdb all the array elements have been initialized to nul:

(gdb) p dest
$8 = {0x0 <repeats 11 times>}

source code:

void copy_characters()
{
    /* static character array */
    char src[] = "sourcefile";
    /* array of pointers to char */
    char *dest[sizeof(src)] = {0};
    size_t i = 0;

    /* Display the source characters */
    for(i = 0; src[i] != '\0'; i++) {
        printf("src [ %c ]\n", src[i]);
    }

    /* Copy the characters */
    for(i = 0; i < sizeof(src); i++) {
        dest[i] = &src[i];
    }

    /* Display them */
    for(i = 0; dest[i] != '\0'; i++) {
        printf("dest: %d [ %s ]\n", i, dest[i]);
    }
}

Many thanks for any suggestions,

+3  A: 
for(i = 0; i < sizeof(src); i++) { 
    dest[i] = &src[i]; 
}

should be

for(i = 0; i < sizeof(src); i++) { 
    dest[i] = src[i]; 
}

You don't need addresses of the source characters...

Edit

If you actually need the addresses, you cannot expect them to be terminated by zero, since you have overwritten all of them (the last one with the address of zero byte of src). Just loop until i == sizeof(dst)/sizeof(*dst).

jpalecek
robUK
+3  A: 

First off - I'm not sure what you're trying to do here. But:

dest[i] is a pointer pointing to a character.

You should test if the value pointed by dest[i] is null, as:

for(i = 0; *(dest[i]) != '\0'; i++) {
adamk
*(dest[i]) is the same as *dest[i]. I thought that dest[i] would dereference the value being pointed out. Because I in my print statement I use dest[i]. Thanks.
robUK
@robUK your print statement expects a string (char*) there is no implicit dereferencing. the condition needs the char and not its address, so you have to dereference it.
josefx
thats clear. Thanks.
robUK
+1  A: 

Change

for(i = 0; dest[i] != '\0'; i++) {
    printf("dest: %d [ %s ]\n", i, dest[i]);
}

To

for(i = 0; i < sizeof(src); i++) {
    printf("dest: %d [ %s ]\n", i, dest[i]);
}

because there are a total of "sizeof(src)" elements in the dest array.

If you test for '\0' you will get a runtime error, because you test if the pointer is equal to '\0' (zero), which will not be the case.

Another way to solve this (for this specific function) is to change the loop to

for(i = 0; *dest[i] != '\0'; i++) {
    printf("dest: %d [ %s ]\n", i, dest[i]);
}

since you expect the last element of dest to point to the last character of the string, which is '\0'.

I would go with the first solution, because it is more general (you iterate 'size of the array' times).

George B.