Why would this work: Array of pointers to C strings? -


why such checking *(string + i) don't pass after strings printed? output of printf("%p", string + + 1); shows string + + 1 isn't null.
i've tried code several times several quantity of strings.
maybe of c guru can give answer this? in advance. :-)

the code:

#include <stdio.h>  int main(void) {         size_t i;         char *string[] = {                 "hey, baby!",                 "how ya?",                 "what heck going on in here?"         };          (i = 0; *(string + i); ++i)         {                 printf("%s\t%p\n", *(string + i), string + i);         }          printf("%p", string + + 1);          return 0; } 

the output:

[aenry@mintk50id 2]$ ./test    hey, baby!  0x7fff691978e0 how ya? 0x7fff691978e8 heck going on in here?  0x7fff691978f0 0x7fff69197900%  [aenry@mintk50id 2]$ gcc -v    ... gcc version 4.8.1 (ubuntu/linaro 4.8.1-10ubuntu9) 

--------edit: turned out, junk code , somehow gcc compiles in way works. ub nuff said. thanks, guys.

you can't expect work: for (i = 0; *(string + i); ++i).

the fact each string ends 0 character, doesn't mean array of strings ends null pointer.

so *(string + i) illegal memory access i becomes larger 2.

change above to: for (i=0; i<sizeof(string)/sizeof(*string); ++i).


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -