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. Advertisements

  2. Brian Stubblefield

    Kevin C. Guest



    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. Advertisements

  3. Brian Stubblefield

    Eric Sosman Guest

    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.
    The initialization is pointless, because you're about
    to assign new values to `i' in the immediately succeeding
    `for' loop.
    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."


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




    <<Remove the del for email>>
     
    Barry Schwarz, May 26, 2004
    #4
  5. At least 3 of the above are unused.
    The use of 'short int' is probably a false economy.
    The line above indicates a lack of understanding of subscripts in
    C.


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

    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. 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. For a scalar, the braces are optional. It means the same
    thing as short int i = 0;
     
    Michael Fyles, May 30, 2004
    #8
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.