![]() |
incompatible pointer assignment ?
Hello,
I have a question; Can someone explain this beginning programmer why the following pointer `ptr' assigment to the array of pointers to strings `*palpha[]' won't work ? #include <stdio.h> #include <ctype.h> int main(void) { const char *palpha[]= { "alpha", "bravo", "charlie", "delta" }; char *ptr = NULL; int i = 0; for(i = 0; i < 4; i++) { /* this doesn't seem to work ? */ ptr = palpha[i]; while(*ptr++) printf("%c", toupper(*ptr)); printf("\n"); } return 0; } If anyone could explain the reasoning behind my faillure I would be greatfull.... Thnkx.. J. |
Re: incompatible pointer assignment ?
zmbdcqnrdfetnws wrote: <snip> > int main(void) { > const char *palpha[]= { "alpha", "bravo", "charlie", "delta" }; > char *ptr = NULL; const char *ptr; <snip> > while(*ptr++) > printf("%c", toupper(*ptr)); while (*ptr) printf("%c", toupper(*ptr++)); <snip> Your palpha is an arry of const strings, so ptr should point to a const string, then in your loop, you incremented ptr before the first call to toupper. Regards, Ed. |
Re: incompatible pointer assignment ?
zmbdcqnrdfetnws wrote:
> Hello, > > I have a question; Can someone explain this beginning > programmer why the following pointer `ptr' assigment > to the array of pointers to strings `*palpha[]' won't work ? > > #include <stdio.h> > #include <ctype.h> > > int main(void) { > const char *palpha[]= { "alpha", "bravo", "charlie", "delta" }; palpha is an array of pointers to const char. > char *ptr = NULL; ptr is a pointer to char (no const). So that is your problem down... > for(i = 0; i < 4; i++) { > /* this doesn't seem to work ? */ > ptr = palpha[i]; ....here. Change the type of the pointer to pointer to const char and you should be set. -- Thomas. |
Re: incompatible pointer assignment ?
In <3fd09123$0$129$e4fe514c@dreader7.news.xs4all.nl > zmbdcqnrdfetnws <lyyymvoefewtglyyyfu@jgtxsriyhrnduxwp.dt> writes:
>I have a question; Can someone explain this beginning >programmer why the following pointer `ptr' assigment >to the array of pointers to strings `*palpha[]' won't work ? > >#include <stdio.h> >#include <ctype.h> > >int main(void) { > const char *palpha[]= { "alpha", "bravo", "charlie", "delta" }; > char *ptr = NULL; > int i = 0; > > for(i = 0; i < 4; i++) { > /* this doesn't seem to work ? */ > ptr = palpha[i]; You may want to use a compiler with better diagnostics: fangorn:~/tmp 125> gcc test.c test.c: In function `main': test.c:11: warning: assignment discards qualifiers from pointer target type This make it quite clear, doesn't it? Either drop the const from the declaration of palpha or add a const in the declaration of ptr. You can also use a cast to make the compiler happy, but that's usually not a good idea. Dan -- Dan Pop DESY Zeuthen, RZ group Email: Dan.Pop@ifh.de |
incompatible pointer assignment - Solved .. thnkx.. !
>> Hello,
>> >> I have a question; Can someone explain this beginning >> programmer why the following pointer `ptr' assigment >> to the array of pointers to strings `*palpha[]' won't work ? <CUT>... </CUT> Thnkx for the quick and clear reply you guys..., I know now what I did wrong and I have solved the problem. Thanks again .. ! J. |
Re: incompatible pointer assignment ?
zmbdcqnrdfetnws <lyyymvoefewtglyyyfu@jgtxsriyhrnduxwp.dt> wrote in
news:3fd09123$0$129$e4fe514c@dreader7.news.xs4all. nl: > Hello, > > I have a question; Can someone explain this beginning > programmer why the following pointer `ptr' assigment > to the array of pointers to strings `*palpha[]' won't work ? That question was answered by others. > while(*ptr++) > printf("%c", toupper(*ptr)); Do you skip the first character of each word intentionally? Sinan. -- A. Sinan Unur asu1@c-o-r-n-e-l-l.edu Remove dashes for address Spam bait: mailto:uce@ftc.gov |
Re: incompatible pointer assignment ?
zmbdcqnrdfetnws wrote:
> > I have a question; Can someone explain this beginning > programmer why the following pointer `ptr' assigment > to the array of pointers to strings `*palpha[]' won't work ? > > #include <stdio.h> > #include <ctype.h> > > int main(void) { > const char *palpha[]= { "alpha", "bravo", "charlie", "delta" }; > char *ptr = NULL; const char *ptr = NULL; ^^^^^ > int i = 0; > > for(i = 0; i < 4; i++) { > /* this doesn't seem to work ? */ > ptr = palpha[i]; > > while(*ptr++) while (*ptr) > printf("%c", toupper(*ptr)); printf("%c", toupper((unsigned char)*ptr++)); ^^^^^^^^^^^^^^^ > > printf("\n"); > } > > return 0; > } Make the indicated changes. The underlined portions are probably not absolutely necessary. Then think about the difference. -- Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net) Available for consulting/temporary embedded and systems. <http://cbfalconer.home.att.net> USE worldnet address! |
Re: incompatible pointer assignment ?
On Fri, 05 Dec 2003 08:24:04 -0600, Ed Morton
<mortonAVOIDINGSPAM@lucent.com> wrote: > > >zmbdcqnrdfetnws wrote: ><snip> >> int main(void) { >> const char *palpha[]= { "alpha", "bravo", "charlie", "delta" }; >> char *ptr = NULL; > >const char *ptr; ><snip> >> while(*ptr++) >> printf("%c", toupper(*ptr)); > >while (*ptr) > printf("%c", toupper(*ptr++)); > ><snip> > >Your palpha is an arry of const strings, so ptr should point to a const Almost. palpha is an array of pointers to const strings. >string, then in your loop, you incremented ptr before the first call to >toupper. > >Regards, > > Ed. <<Remove the del for email>> |
Re: incompatible pointer assignment ?
zmbdcqnrdfetnws wrote:
> Hello, > > I have a question; Can someone explain this beginning > programmer why the following pointer `ptr' assigment > to the array of pointers to strings `*palpha[]' won't work ? #include <stdio.h> #include <ctype.h> int main(void) { const char *palpha[] = { "alpha", "bravo", "charlie", "delta" }; char *ptr = NULL; int i = 0; for (i = 0; i < 4; i++) { /* this doesn't seem to work ? */ /* mha: the palpha[i] point to const char, while ptr points to (non-const) char. You are discarding the constness of the target. That said, it is unclear what you mean by "doesn't seem to work." If the loop following, which skips over the first character, is what "doesn't seem to work," that may be because you are incrementing ptr before use. A replacement follows. */ #if 0 /* OP's code */ ptr = palpha[i]; while (*ptr++) printf("%c", toupper(*ptr)); #endif /* mha: one of many possible replacements, still discarding constness. Fixing that is left as an exercise. */ for (ptr = palpha[i]; *ptr; ptr++) putchar(toupper(*ptr)); printf("\n"); } return 0; } -- Martin Ambuhl |
| All times are GMT. The time now is 02:40 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.