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