2010-08-11 5 views
2

gcc 4.4.4 c89pointeurs vers des pointeurs et des tableaux de pointeurs

Je comprends les pointeurs ok. Cependant, je suis en train de faire des tableaux de pointeurs et des pointeurs vers des pointeurs.

J'ai été déconner avec cet extrait de code et j'ai laissé des commentaires de ce que je crois comprendre.

Un grand merci pour tout conseil si mes commentaires sont corrects avec la ligne de 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++); 
    } 
} 
+0

Est-ce devoir? – PeterK

Répondre

7

Commentaires en dessous de chaque partie (bits je ne suis pas mentionnés sont correctes):

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

Ce tableau est pas statique - il a auto durée de stockage.

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

Ceci est un tableau de pointeurs sur char, pas un pointeur vers un tableau. La longueur du tableau est 7, car le sizeof(src) est 7 (il inclut le terminateur de chaîne nul).

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

Plus précisément, il points au premier caractère src, qui est le 'r' dans "rabbit".

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

Il montre du doigt le premier pointeur dans le tableau dest.

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

Correct - parce que vous n'initialisés dest. Vous pouvez modifier la déclaration de dest à ceci:

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

... et cela fonctionnera.

+0

Qu'est-ce qu'un pointeur? –

1

L'erreur est lorsque vous attribuez dest à ptr_ptr, qui est en fait un tableau non initialisé de pointeurs vers des caractères, en passant par elle withing la boucle while échouera.

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

Il définit les valeurs des 6 premiers membres de 'dest' dans la boucle précédente (la septième reste cependant non initialisée). – caf

+0

vous avez raison, j'ai raté cette partie. Mais il saute en effet encore '\ 0' –

Questions connexes