incompatible pointer assignment ?

Discussion in 'C Programming' started by zmbdcqnrdfetnws, Dec 5, 2003.

  1. 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;

    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.
     
    zmbdcqnrdfetnws, Dec 5, 2003
    #1
    1. Advertising

  2. zmbdcqnrdfetnws

    Ed Morton Guest

    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.
     
    Ed Morton, Dec 5, 2003
    #2
    1. Advertising

  3. 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;


    ....here.

    Change the type of the pointer to pointer to const char and you should
    be set.

    --
    Thomas.
     
    Thomas Stegen, Dec 5, 2003
    #3
  4. zmbdcqnrdfetnws

    Dan Pop Guest

    In <3fd09123$0$129$4all.nl> zmbdcqnrdfetnws <> 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;


    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, Dec 5, 2003
    #4
  5. zmbdcqnrdfetnws

    zbqdxdm Guest

    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.
     
    zbqdxdm, Dec 5, 2003
    #5
  6. zmbdcqnrdfetnws <> wrote in
    news:3fd09123$0$129$4all.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

    Remove dashes for address
    Spam bait: mailto:
     
    A. Sinan Unur, Dec 5, 2003
    #6
  7. zmbdcqnrdfetnws

    CBFalconer Guest

    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;
    >
    > 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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Dec 5, 2003
    #7
  8. On Fri, 05 Dec 2003 08:24:04 -0600, Ed Morton
    <> 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>>
     
    Barry Schwarz, Dec 6, 2003
    #8
  9. 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 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;
    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; *ptr; ptr++)
    putchar(toupper(*ptr));

    printf("\n");
    }

    return 0;
    }








    --
    Martin Ambuhl
     
    Martin Ambuhl, Dec 6, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dimitris Mandalidis

    Re: malloc & incompatible types in assignment

    Dimitris Mandalidis, Aug 31, 2003, in forum: C Programming
    Replies:
    4
    Views:
    1,014
    Al Bowers
    Aug 31, 2003
  2. gk245
    Replies:
    10
    Views:
    1,020
    Peter Shaggy Haywood
    Apr 22, 2006
  3. Olaf \El Blanco\

    assignment from incompatible pointer type

    Olaf \El Blanco\, Jun 10, 2006, in forum: C Programming
    Replies:
    1
    Views:
    522
    Olaf \El Blanco\
    Jun 10, 2006
  4. Bart Vandewoestyne

    assignment from incompatible pointer type

    Bart Vandewoestyne, Apr 27, 2010, in forum: C Programming
    Replies:
    2
    Views:
    2,267
    Eric Sosman
    Apr 27, 2010
  5. Prathamesh Kulkarni

    incompatible pointer assignment

    Prathamesh Kulkarni, Dec 10, 2012, in forum: C Programming
    Replies:
    7
    Views:
    327
    Keith Thompson
    Dec 10, 2012
Loading...

Share This Page