incompatible types in assignment

Discussion in 'C Programming' started by v4vijayakumar, Jun 5, 2006.

  1. Whats wrong with the code in line no. 7?!

    #cat test3.c
    #include <stdio.h>

    int main(int argc, char *argv[])
    {
    int ia1[10] = {0, 1, 2};
    int ia2[10];
    ia2 = ia1;

    printf("%d\n", ia2[2]);

    return 0;
    }
    #gcc test3.c
    test3.c: In function `main':
    test3.c:7: incompatible types in assignment
    #
     
    v4vijayakumar, Jun 5, 2006
    #1
    1. Advertisements

  2. v4vijayakumar said:
    You can't assign to arrays in C. They are lvalues, but not "modifiable
    lvalues".

    Fix:

    #include <string.h>

    and then, provided the two arrays are of the same size and type, you can
    replace your assignment attempt with this::

    memcpy(ia2, ia1, sizeof ia2);
     
    Richard Heathfield, Jun 5, 2006
    #2
    1. Advertisements

  3. v4vijayakumar

    maurice.gray Guest

    Also.

    int main(int argc, char *argv[])
    {
    int ia1[10] = {0, 1, 2};
    int *ia2;
    ia2 = ia1;

    printf("%d\n",*ia2+2);

    return 0;

    }
     
    maurice.gray, Jun 5, 2006
    #3
  4. v4vijayakumar

    maurice.gray Guest

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
    int ia1[10] = {0, 1, 2};
    int *ia2;
    ia2 = ia1;

    printf("%d\n",*(ia2+2));

    return 0;

    }
     
    maurice.gray, Jun 5, 2006
    #4
  5. v4vijayakumar

    Haider Guest

    array name is not a varaiable so you can't assign i.e. ia2=ia1; or
    perform ia2++. refer to K&R (2nd) section 5.3.
    above code will work with following fix
    in place of int ia2[10]; use int *ia2.
    rest is fine.
     
    Haider, Jun 5, 2006
    #5
  6. v4vijayakumar

    CBFalconer Guest

    Arrays are not fundamental objects, and cannot be bodily assigned
    in C. You could write:

    for (i = 0; i < 10; i++) ia1 = ia2;

    after declaring i, or use such routines as memcpy. These copy the
    elements one by one.

    --
    "Our enemies are innovative and resourceful, and so are we.
    They never stop thinking about new ways to harm our country
    and our people, and neither do we." -- G. W. Bush.
    "The people can always be brought to the bidding of the
    leaders. All you have to do is tell them they are being
    attacked and denounce the pacifists for lack of patriotism
    and exposing the country to danger. It works the same way
    in any country." --Hermann Goering.
     
    CBFalconer, Jun 5, 2006
    #6
  7. v4vijayakumar

    Nelu Guest

    It depends what he wants to do.
    This doesn't copy the elements so, a change to ia2
    will reflect in ia1. If he wants to change values in
    ia2 and keep the ia1 unchanged he should either follow
    Richard Heathfield's example, or copy the values one by
    one in a for loop.
     
    Nelu, Jun 5, 2006
    #7
  8. v4vijayakumar

    whyglinux Guest

    ia1, which is used as an rvalue, is converted to type int*. On the
    other hand, ia2, as an lvalue, is of type int[10]. Since int* can never
    be converted to int[10] (while int[10] to int* conversion is defined),
    the compiler complains.
     
    whyglinux, Jun 5, 2006
    #8
  9. v4vijayakumar

    whyglinux Guest

    Pointers, structs and unions, as well as arrays, are all derived (not
    fundamental) types, but they are assignable while arrays are not.
     
    whyglinux, Jun 5, 2006
    #9
  10. v4vijayakumar

    whyglinux Guest

    The defination of variable is not given and seldom used in the
    standard because of its ambiguity. At least, variable can refer to one
    of the following 3 meanings:

    1. Named objects that can only be modifiable.
    2. Named objects (modifiable and non-modifiable)
    3. Objects (both named and unamed).

    So I can not say you and K&R are wrong when saying "array name is not a
    variable" as it can be interpreted as "array name is not modifiable".
    However, I prefer that array names are variables (if we do not care
    whether they can be changed or not) as that also said in K&R (2nd).
    Yes, I am sure you can find such descriptions in the book.

    These indicate that the meaning of variable used in K&R (2nd) is
    inconsistent.

    As to why ia2=ia1 is invalid, follow what Richard Heathfield have said.
     
    whyglinux, Jun 5, 2006
    #10
  11. v4vijayakumar

    CBFalconer Guest

    Alright, if you insist, go ahead and make assignments to arrays. I
    predict they won't work in C. Note that a struct or union actually
    defines a new type, an array only defines an aggregate of an old
    type.

    --
    "Our enemies are innovative and resourceful, and so are we.
    They never stop thinking about new ways to harm our country
    and our people, and neither do we." -- G. W. Bush.
    "The people can always be brought to the bidding of the
    leaders. All you have to do is tell them they are being
    attacked and denounce the pacifists for lack of patriotism
    and exposing the country to danger. It works the same way
    in any country." --Hermann Goering.
     
    CBFalconer, Jun 5, 2006
    #11
  12. I think you misread "...while arrays are not."

    - Bill
     
    William J. Leary Jr., Jun 5, 2006
    #12
  13. He knows that; he specifically acknowledged that arrays are not
    assignable.
    Structs, unions, and arrays are all aggregates defined in terms of
    existing types. The standard referss to struct, union, array, and
    function types as "derived types" in C99 6.2.5.

    An array type is certainly a type. It's probably not a "first-class"
    or "fundamental" type, because, among other things, it doesn't support
    assignment or comparison, but C doesn't talk about "first-class" or
    "fundamental" types. Structures can be assigned and arrays can't, but
    C doesn't make any strong fundamental distinction between them on that
    basis; they just happen to support different sets of operations.

    C99 6.2.5p21:

    Arithmetic types and pointer types are collectively called _scalar
    types_. Array and structure types are collectively called
    _aggregate types_.

    and a footnote:

    Note that aggregate type does not include union type because an
    object with union type can only contain one member at a time.

    (So can a single-element structure, but that's not a big deal; most
    references in the standard to "aggregate types" talk about "aggregate
    or union" types.)
     
    Keith Thompson, Jun 6, 2006
    #13
  14. v4vijayakumar

    Jack Klein Guest

    You are completely incorrect about pointers. They are not derived
    types at all, they are fundamental scalar types in C. If you believe
    differently, kindly cite the section from the C standard that supports
    your position.
     
    Jack Klein, Jun 6, 2006
    #14
  15. v4vijayakumar

    pete Guest

    N869
    6.2.5 Types

    [#19] Any number of derived types can be constructed from
    the object, function, and incomplete types, as follows:

    -- A pointer type may be derived from a function type, an
    object type, or an incomplete type, called the
    referenced type.
     
    pete, Jun 6, 2006
    #15
  16. v4vijayakumar

    Suman Guest

    N869
    [#20]These methods of constructing *derived types*[1] can be applied
    recursively.

    [1]Emphasis mine. That's it -- I'd think.
     
    Suman, Jun 6, 2006
    #16
  17. They're scalar types *and* derived types. (pete already posted the
    relevant citation.)

    Note that the standard never uses the term "fundamental" in this
    context.
     
    Keith Thompson, Jun 6, 2006
    #17
    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.