incompatible types in assignment

Discussion in 'C Programming' started by Brian Stubblefield, May 25, 2004.

  1. Dear clc members,

    I am new to C and am posting several messages concerning a large C
    program that I am debugging.

    I am encountering a "incompatible types in assignment" warning for the
    following code during the compile stage:

    #include <stdio.h>
    #include <signal.h>
    #include <stdlib.h>
    #include <string.h>

    int main(void)
    {

    /* removed code */

    short int n = 10;
    char key[n][6];
    short int i = { 0};

    for(i = 1; i <= n; i++)
    {
    key = ' '; /* incompatible types in assignment */
    {

    /* removed code */
    }

    If there is a similar posting that provides the answer(s), please
    point me in the direction. I really do not know what look for in my
    search.

    Any help is greatly appreciated.

    Thank you,

    Brian
    Brian Stubblefield, May 25, 2004
    #1
    1. Advertising

  2. Brian Stubblefield

    Kevin C. Guest

    "Brian Stubblefield" <> wrote in message
    news:...
    > key = ' '; /* incompatible types in assignment */


    key is a 2D array. Since you are only referencing one dimension, what you
    end up getting is a char pointer to the underlying array, and hence this
    statement is trying to assign a char constant to a char pointer. You can
    either use double quotes to change the constant to a C-style string or you
    can explicitly reference key[0] which is what I assume you intended.
    Kevin C., May 25, 2004
    #2
    1. Advertising

  3. Brian Stubblefield

    Eric Sosman Guest

    Brian Stubblefield wrote:
    > Dear clc members,
    >
    > I am new to C and am posting several messages concerning a large C
    > program that I am debugging.
    >
    > I am encountering a "incompatible types in assignment" warning for the
    > following code during the compile stage:
    >
    > #include <stdio.h>
    > #include <signal.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > int main(void)
    > {
    >
    > /* removed code */
    >
    > short int n = 10;
    > char key[n][6];


    If this gets through the compiler, it's either a very
    new ("C99") implementation or you're using some compiler-
    specific extensions to Standard C. Prior to the C99 edition
    of the Standard, array dimensions could only be constants,
    and you would have to write `char key[10][6];' instead.

    > short int i = { 0};


    The initialization is pointless, because you're about
    to assign new values to `i' in the immediately succeeding
    `for' loop.

    > for(i = 1; i <= n; i++)


    The limits on `i' are suspect. In C, an array of ten
    elements has indices [0], [1], and so on through [9], but
    *not* [10]. If this is not yet second nature to you, I
    suggest that you are not yet equipped to debug "a large
    C program" and you are likely to create more trouble than
    you solve. Seriously. Think "novice sword-swallower on
    karaoke night."

    > {
    > key = ' '; /* incompatible types in assignment */


    What is `key'? It is an array of ten elements. What
    are those elements? Each is an array of six `char'. In
    particular, `key' (for legal values of `i') is an array
    of six `char'.

    What is the thing on the right-hand side? A single
    `char'. (Pedants would say it's an `int' and they'd be right,
    but at your present stage of development I think you'd find
    the knowledge a distraction. "It's a `char', and damn the
    torpedoes.)

    Now: Is an array of six somethings the same as a single
    something? No, so the attempted assignment makes no sense.
    The stuff on the r.h.s. must have the same type as the thing
    on the l.h.s., or at least be convertible to it.

    As in your other recent difficulty, you're now faced with
    trying to figure out what the code's author was trying to
    accomplish, after which you need to write a correct expression
    of that intention. The original author has said "Gzorgflash"
    and it's up to you to discover that he really meant "O for a
    Muse of fire!" There is no way any expert or quasi-expert out
    here in Usenetland can do this for you given only the original
    "Gzorgflash" -- and if the program is as large as you say (and
    as badly written as the snippets suggest), I doubt anyone will
    undertake the task for free.

    > {
    >
    > /* removed code */
    > }
    >
    > If there is a similar posting that provides the answer(s), please
    > point me in the direction. I really do not know what look for in my
    > search.
    >
    > Any help is greatly appreciated.


    My sincere advice is to hire someone who's C knowledge
    exceeds yours. At this point it seems you are a beginner, and
    you've been dropped into the deep end without a life jacket.
    You are not going to make much progress unaided, and the amount
    of aid you require is probably greater than a channel like
    Usenet can provide. "Get professional help."

    --
    Eric Sosman, May 25, 2004
    #3
  4. On Tue, 25 May 2004 21:32:19 GMT, "Kevin C." <> wrote:

    >
    >"Brian Stubblefield" <> wrote in message
    >news:...
    >> key = ' '; /* incompatible types in assignment */

    >
    >key is a 2D array. Since you are only referencing one dimension, what you
    >end up getting is a char pointer to the underlying array, and hence this


    No, key is not a pointer. key is the i-th element of key. You
    snipped it from the OP's code but key is an array of n arrays of 6
    char. Therefore, key is the i-th array of 6 char. It is an array.

    >statement is trying to assign a char constant to a char pointer. You can
    >either use double quotes to change the constant to a C-style string or you


    An array is not a modifiable lvalue. Therefore it cannot appear on
    the left of an assignment operator. Changing the character constant
    to a string literal will not make the assignment legal.

    >can explicitly reference key[0] which is what I assume you intended.
    >




    <<Remove the del for email>>
    Barry Schwarz, May 26, 2004
    #4
  5. Brian Stubblefield wrote:
    > Dear clc members,
    >
    > I am new to C and am posting several messages concerning a large C
    > program that I am debugging.
    >
    > I am encountering a "incompatible types in assignment" warning for the
    > following code during the compile stage:
    >
    > #include <stdio.h>
    > #include <signal.h>
    > #include <stdlib.h>
    > #include <string.h>


    At least 3 of the above are unused.

    >
    > int main(void)
    > {
    >
    > /* removed code */
    >
    > short int n = 10;
    > char key[n][6];
    > short int i = { 0};


    The use of 'short int' is probably a false economy.

    >
    > for(i = 1; i <= n; i++)


    The line above indicates a lack of understanding of subscripts in
    C.
    > {
    > key = ' '; /* incompatible types in assignment */


    The above indicates a confusion between scalar characters and an
    array of char.

    > {
    >
    > /* removed code */
    > }



    Perhaps you meant something like:


    #include <string.h>

    int main(void)
    {
    size_t n = 10, i;
    char key[n][6];

    for (i = 0; i < n; i++)
    strcpy(key, " ");

    return 0;
    }
    Martin Ambuhl, May 26, 2004
    #5
  6. Thank you all for your programming suggestions and advice. I have
    been able to resolve the warning messages.
    Brian Stubblefield, May 26, 2004
    #6
  7. On 25 May 2004 -0700, (Brian Stubblefield) wrote:
    >Dear clc members,
    >
    >I am new to C and am posting several messages concerning a large C
    >program that I am debugging.
    >
    >I am encountering a "incompatible types in assignment" warning for the
    >following code during the compile stage:
    >
    >#include <stdio.h>
    >#include <signal.h>
    >#include <stdlib.h>
    >#include <string.h>
    >
    >int main(void)
    >{
    >
    > /* removed code */
    >
    > short int n = 10;
    > char key[n][6];
    > short int i = { 0};


    what does it means
    short int i = { 0 }
    I have the mean only for
    short int i[1]={0};
    RoSsIaCrIiLoIA, May 30, 2004
    #7
  8. RoSsIaCrIiLoIA <> wrote:
    > On 25 May 2004 -0700, (Brian Stubblefield) wrote:
    > >#include <stdio.h>
    > >#include <signal.h>
    > >#include <stdlib.h>
    > >#include <string.h>
    > >
    > >int main(void)
    > >{
    > >
    > > /* removed code */
    > >
    > > short int n = 10;
    > > char key[n][6];
    > > short int i = { 0};

    >
    > what does it means
    > short int i = { 0 }
    > I have the mean only for
    > short int i[1]={0};


    For a scalar, the braces are optional. It means the same
    thing as short int i = 0;

    --
    Michael
    Michael Fyles, May 30, 2004
    #8
    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,004
    Al Bowers
    Aug 31, 2003
  2. v4vijayakumar

    incompatible types in assignment

    v4vijayakumar, Jun 5, 2006, in forum: C Programming
    Replies:
    16
    Views:
    906
    Keith Thompson
    Jun 6, 2006
  3. Richard Bos

    error: incompatible types in assignment

    Richard Bos, Jul 25, 2006, in forum: C Programming
    Replies:
    8
    Views:
    1,488
    Mark McIntyre
    Jul 25, 2006
  4. Replies:
    3
    Views:
    805
  5. dannydb368

    HELP please - incompatible types in assignment

    dannydb368, Mar 15, 2009, in forum: C Programming
    Replies:
    0
    Views:
    1,053
    dannydb368
    Mar 15, 2009
Loading...

Share This Page