440,559 Members | 1,180 Online Need help? Post your question and get tips & solutions from a community of 440,559 IT Pros & Developers. It's quick & easy.

# array of char pointers

 P: n/a And I thought I understood it, finally. Alas. given: char *s[]={"Jan","Feb","Mar","April"}; is it possible to have char *p point at s? *p = s...does not do it, as I expect. *p=s...I believe sets it to point to the first element in s. But this is not what I want. What I want is a pointer which, when incremented will produce "Jan","Feb","Mar","April" etc. I believe what I want is a pointer equivalent of: printf("%s", s[i]), (where i = 0->3) Thanks. Apr 18 '07 #1
14 Replies

 P: n/a mdh wrote: And I thought I understood it, finally. Alas. given: char *s[]={"Jan","Feb","Mar","April"}; is it possible to have char *p point at s? It might be conceptually easier to think of s as a char**. so given char* p; p = s; p now points to the string literal "Jan". *p = s...does not do it, as I expect. *p=s...I believe sets it to point to the first element in s. But this is not what I want. What I want is a pointer which, when incremented will produce "Jan","Feb","Mar","April" etc. Then you want p to be another char**. I believe what I want is a pointer equivalent of: printf("%s", s[i]), (where i = 0->3) char** p = s; for( unsigned n = 0; n < 4; ++n ) { printf("%s", *p ); ++p; } -- Ian Collins. Apr 18 '07 #2

 P: n/a On Apr 17, 6:59 pm, Ian Collins It might be conceptually easier to think of s as a char**. Then you want p to be another char**. char** p = s; for( unsigned n = 0; n < 4; ++n ) { printf("%s", *p ); ++p; Ian...got "JanJanJan" if I replaced *p with *p++ got as expected. Does this make sense? Apr 18 '07 #3

 P: n/a mdh 3) #include #define countof(X) ( (size_t) ( sizeof(X)/sizeof*(X) ) ) int main(void) { const char *s[]={"Jan","Feb","Mar","April"}; const char **p; size_t i; puts("Loop 1:"); for (i = 0; i < countof(s); i++) puts(s[i]); puts("\nLoop 2:"); for (p = s, i = 0; i < countof(s); i++) puts(p[i]); puts("\nLoop 3:"); for (p = s; p < &s[countof(s)]; p++) puts(*p); return 0; } Apr 18 '07 #4

 P: n/a mdh wrote: > >>It might be conceptually easier to think of s as a char**.Then you want p to be another char**. >char** p = s;for( unsigned n = 0; n < 4; ++n ){ printf("%s", *p ); ++p; Ian...got "JanJanJan" if I replaced *p with *p++ got as expected. Does this make sense? I didn't test what I posted, but I just tried this and it worked as expected. There shouldn't be any difference. #include int main(void) { char *s[]={"Jan","Feb","Mar","April"}; char** p = s; for( unsigned n = 0; n < 4; ++n ) { printf("%s\n", *p ); ++p; } } -- Ian Collins. Apr 18 '07 #5

 P: n/a On Apr 17, 7:11 pm, Peter Nilsson #include #define countof(X) ( (size_t) ( sizeof(X)/sizeof*(X) ) ) int main(void) { const char *s[]={"Jan","Feb","Mar","April"}; const char **p; size_t i; puts("Loop 1:"); for (i = 0; i < countof(s); i++) puts(s[i]); puts("\nLoop 2:"); for (p = s, i = 0; i < countof(s); i++) puts(p[i]); puts("\nLoop 3:"); for (p = s; p < &s[countof(s)]; p++) puts(*p); return 0; } Thank you....and thank you for the code..I will follow along in the debugger. Michael. Apr 18 '07 #6

 P: n/a On Apr 17, 7:11 pm, Peter Nilsson

 P: n/a mdh said: On Apr 17, 7:11 pm, Peter Nilsson Thus, to point to an element of an array of char *, you needa pointer to char *. In other words, you need a char **. Is this thus the equivalent of *argv[] which we see in main? No. The only place that there is an exact equivalence between T ** and T *[] is in a formal parameter declaration, and that's only for hysterical reasons. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Apr 18 '07 #8

 P: n/a mdh said: Is this thus the equivalent of *argv[] which we see in main? Richard Heathfield

 P: n/a mdh wrote: On Apr 17, 6:59 pm, Ian Collins

 P: n/a mdh wrote: And I thought I understood it, finally. Alas. given: char *s[]={"Jan","Feb","Mar","April"}; is it possible to have char *p point at s? *p = s...does not do it, as I expect. *p=s...I believe sets it to point to the first element in s. But this is not what I want. What I want is a pointer which, when incremented will produce "Jan","Feb","Mar","April" etc. I believe what I want is a pointer equivalent of: printf("%s", s[i]), (where i = 0->3) #include #include int main(void) { char *s[] = { "Jan", "Feb", "Mar", "April" }; char *p, **q, *endp; size_t ns = sizeof s / sizeof *s, i; printf("[Output for one implementation:\n\n"); printf("The original array has these contents\n"); for (i = 0; i < ns; i++) printf("s[%zu] @ %p: \"%s\"\n", i, (void *) s[i], s[i]); putchar('\n'); printf("p is a pointer to char, so incrementing it leads\n" "to changes of 1 byte. For example, with\n" "initialization of p=s, we can get:\n"); for (p = s, endp = strchr(s, 0); p <= endp; p++) printf("@ %p: %o %c\n", (void *) p, *p, *p ? *p : ' '); putchar('\n'); printf("q is a pointer to pointer to char, so incrementing it\n" "leads to changes of 1 pointer. For example, with\n" "initialization of q=s, we can get:\n"); for (q = s; q <= &s[ns - 1]; q++) printf("q = %p, *q = %p: \"%s\"\n", (void *) q, (void *) *q, *q); putchar('\n'); return 0; } [Output for one implementation: The original array has these contents s @ 1df0: "Jan" s @ 1df4: "Feb" s @ 1df8: "Mar" s @ 1dfc: "April" p is a pointer to char, so incrementing it leads to changes of 1 byte. For example, with initialization of p=s, we can get: @ 1df0: 112 J @ 1df1: 141 a @ 1df2: 156 n @ 1df3: 0 q is a pointer to pointer to char, so incrementing it leads to changes of 1 pointer. For example, with initialization of q=s, we can get: q = dff98, *q = 1df0: "Jan" q = dff9c, *q = 1df4: "Feb" q = dffa0, *q = 1df8: "Mar" q = dffa4, *q = 1dfc: "April" Apr 18 '07 #11

 P: n/a On Apr 17, 10:16 pm, Martin Ambuhl The original array has these contents s @ 1df0: "Jan" s @ 1df4: "Feb" s @ 1df8: "Mar" s @ 1dfc: "April" p is a pointer to char, so incrementing it leads to changes of 1 byte. For example, with initialization of p=s, we can get: @ 1df0: 112 J @ 1df1: 141 a @ 1df2: 156 n @ 1df3: 0 q is a pointer to pointer to char, so incrementing it leads to changes of 1 pointer. For example, with initialization of q=s, we can get: q = dff98, *q = 1df0: "Jan" q = dff9c, *q = 1df4: "Feb" q = dffa0, *q = 1df8: "Mar" q = dffa4, *q = 1dfc: "April" Thank You Martin, It's beginning to make a lot more sense now. Apr 18 '07 #12

 P: n/a On Apr 17, 10:16 pm, Martin Ambuhl p is a pointer to char, so incrementing it leads q is a pointer to pointer to char, so incrementing it Martin, Could you clear up one last confusing issue...( well, I guess, not last, but confusing :-)) The reason this whole question arose was to understand *argv[] (in main) and try and create an analagous situation to it. On p 114 (K&R) it says *argv[].....is a pointer to an array of character strings" As I asked somewhat cryptically earlier, (which was answered), just to clarify, where does this fit in? I would guess that *argv[] under this circumstance is type pointer to pointer to char? Which, if this is true, should also be allowed to be written as **argv ? Not sure why it is not thus written then. Once again...thanks for taking the time to clear up something which I guess all newbies struggle with. Apr 18 '07 #13

 P: n/a mdh wrote: > The reason this whole question arose was to understand *argv[] (in main) and try and create an analagous situation to it. On p 114 (K&R) it says *argv[].....is a pointer to an array of character strings" As I asked somewhat cryptically earlier, (which was answered), just to clarify, where does this fit in? I would guess that *argv[] under this circumstance is type pointer to pointer to char? Which, if this is true, should also be allowed to be written as **argv ? Not sure why it is not thus written then. It often is. It's certainly my preferred form. -- Ian Collins. Apr 18 '07 #14

 P: n/a mdh wrote: I would guess that *argv[] under this circumstance is type pointer to pointer to char? Which, if this is true, should also be allowed to be written as **argv ? Not sure why it is not thus written then. On Apr 18, 12:40 am, Ian Collins It often is. It's certainly my preferred form. Ok...well that clears that up...thank you. Apr 18 '07 #15

### This discussion thread is closed

Replies have been disabled for this discussion. 