tags:

views:

130

answers:

3

Hello,

gcc 4.4.4 c89

I understand pointers ok. However, I am stepping up to pointer arrays and pointers to pointers.

I have been messing around with this code snippet and have left comments of what I think I understand.

Many thanks for any advice if my comments are correct with the line of code?

void increment_ptr()
{
    /* Static char array */
    char src[] = "rabbit";
    /* pointer to array of pointers to char's - create 6 pointers in this array */
    char *dest[sizeof(src)];
    size_t i = 0;

    /* pointer to a char */
    char* chr_ptr = NULL;
    /* pointer to pointer that points to a char */
    char** ptr_ptr = NULL;

    /* chr_ptr pointer now points to the memory location where 'rabbit' is stored. */
    chr_ptr = src;
    /* ptr_ptr points to the first memory address of the pointer array of where dest is stored */
    ptr_ptr = dest;

    /* Deference chr_ptr and keep going until nul is reached 'rabbit\0'  */
    while(*chr_ptr != '\0')
        /* deference ptr_ptr and assign the address of each letter to the momory location where
           ptr_ptr is currently pointing to. */
        *ptr_ptr++ = chr_ptr++;

    /* reset the ptr_ptr to point to the first memory location 'rabbit' */
    ptr_ptr = dest;

    /* Keep going until NULL is found - However, my program never finds it, ends in UB */
    while(ptr_ptr != NULL) {
        /* Dereference what the pointer to pointer is pointing at the memory lcoation */
        printf("[ %s ]\n", *ptr_ptr++);
    }
}
+4  A: 

Comments below each part (bits I haven't mentioned are correct):

/* Static char array */
char src[] = "rabbit";

This array is not static - it has auto storage duration.

/* pointer to array of pointers to char's - create 6 pointers in this array */
char *dest[sizeof(src)];

This is an array of pointers to char, not a pointer to an array. The length of the array is 7, because the sizeof(src) is 7 (it includes the nul string terminator).

/* chr_ptr pointer now points to the memory location where 'rabbit' is stored. */
chr_ptr = src;

More precisely, it points at the first character in src, which is the 'r' in "rabbit".

/* ptr_ptr points to the first memory address of the pointer array of where dest is stored */
ptr_ptr = dest;

It points at the first pointer in the dest array.

/* Keep going until NULL is found - However, my program never finds it, ends in UB */
while(ptr_ptr != NULL) {

Correct - because you never initialised dest. You could change the declaration of dest to this:

char *dest[sizeof(src)] = { 0 };

...and it will work.

caf
+1  A: 

The error is when you assign dest to ptr_ptr, which is actually an uninitialized array of pointers to chars, going through it withing the while loop will fail.

/* reset the ptr_ptr to point to the first memory location 'rabbit' */
ptr_ptr = dest;

/* Keep going until NULL is found - However, my program never finds it, ends in UB */
while(ptr_ptr != NULL) {
    /* Dereference what the pointer to pointer is pointing at the memory lcoation */
    printf("[ %s ]\n", *ptr_ptr++);
}
Nicolas C.
He sets the values of the first 6 members of `dest` in the previous loop (the seventh remains uninitialised, though).
caf
you're right, I missed that part. But he indeed still skips '\0'
Nicolas C.
+4  A: 

I would suggest that you read Section 6 of the online C-FAQ: 6. Arrays and Pointers

Robert S. Barnes