error: incompatible types in assignment

Discussion in 'C Programming' started by Richard Bos, Jul 25, 2006.

  1. Richard Bos

    Richard Bos Guest

    "" <> wrote:

    > I have the following source code. It seems wierd to me why gca's value
    > cannot be reassigned. It's afterall a pointer and has a pointer value.


    > char gca[7];


    That's not a pointer.

    No, it's not even a "constant pointer".

    RTFFAQFFS: <http://c-faq.com/aryptr/index.html>.

    Richard
     
    Richard Bos, Jul 25, 2006
    #1
    1. Advertising

  2. Richard Bos

    Guest

    I have the following source code. It seems wierd to me why gca's value
    cannot be reassigned. It's afterall a pointer and has a pointer value.
    I am aware that the standard says it's not allowed. But I cannot
    convince myself why this is prohibited.

    static int ia[4] = {64, 64, 64, 64};
    static char ca[4] = {'a', 'b', 'c', 'd'};
    static int iaa[] = {64, 64, 64, 64};
    static char caa[] = {'a', 'b', 'c', 'd'};
    static char cab[] = "abcd";
    static char * cac = "abcd";

    char gca[7];
    char gcaa[] = "hello!";

    int x[4];
    int * px;
    int main(void){
    char sgca[7];
    char sgcaa[] = "hello!";
    //cab = "xyz";
    cab[0] = 'x';
    cac = "xyz";

    gca = gcaa;
    //sgca = sgcaa;
    // gcaa = gca;

    px = &main;
    }


    gcc -o a ary.c
    ary.c: In function `main':
    ary.c:20: error: incompatible types in assignment
    ary.c:24: warning: assignment from incompatible pointer type
     
    , Jul 25, 2006
    #2
    1. Advertising

  3. Richard Bos

    Eric Sosman Guest

    wrote On 07/25/06 10:31,:
    > I have the following source code. It seems wierd to me why gca's value
    > cannot be reassigned. It's afterall a pointer and has a pointer value.
    > I am aware that the standard says it's not allowed. But I cannot
    > convince myself why this is prohibited.
    > [snipped attempt to assign `array1 = array2']


    An array is not a pointer. Please see Section 6 of
    the comp.lang.c Frequently Asked Questions (FAQ) list

    http://www.c-faq.com/

    .... with special attention to Questions 6.5 and 6.7
    through 6.10.

    --
     
    Eric Sosman, Jul 25, 2006
    #3
  4. Richard Bos

    Guest

    Richard Bos wrote:
    > "" <> wrote:
    >
    > > I have the following source code. It seems wierd to me why gca's value
    > > cannot be reassigned. It's afterall a pointer and has a pointer value.

    >
    > > char gca[7];

    >
    > That's not a pointer.
    >
    > No, it's not even a "constant pointer".
    >
    > RTFFAQFFS: <http://c-faq.com/aryptr/index.html>.
    >
    > Richard


    Arrays are ``second-class citizens'' in C; one upshot of this prejudice
    is that you cannot assign to them.

    Well, I guess it's just reiterating my confusion. Why should arrays be
    second class citizens? gca does have a memory location. The value of
    gca has to be stored in memory so gca[0] etc can be accessed.
     
    , Jul 25, 2006
    #4
  5. Richard Bos

    Simon Biber Guest

    wrote:
    > I have the following source code. It seems weird to me why gca's value
    > cannot be reassigned. It's after all a pointer and has a pointer value.
    > I am aware that the standard says it's not allowed. But I cannot
    > convince myself why this is prohibited.


    gca is not a pointer. It is an array. An array is an area of memory. The
    area of memory stores 7 char objects. No pointer to char is stored in
    memory associated with gca.

    In most cases when you use gca as an identifier in your program, the
    compiler substitutes the name of the array for a pointer to the first
    element. It doesn't generate code to read that pointer out of memory. It
    simply writes the pointer value into the generated code, since it knows
    where the array will be placed in memory.

    The C standard says that the name of an array is an lvalue, but not a
    modifiable lvalue. That is, you are not allowed to modify gca. You
    cannot use it on the left hand side of an assignment operator.

    > static int ia[4] = {64, 64, 64, 64};
    > static char ca[4] = {'a', 'b', 'c', 'd'};
    > static int iaa[] = {64, 64, 64, 64};
    > static char caa[] = {'a', 'b', 'c', 'd'};
    > static char cab[] = "abcd";
    > static char * cac = "abcd";
    >
    > char gca[7];
    > char gcaa[] = "hello!";
    >
    > int x[4];
    > int * px;
    > int main(void){
    > char sgca[7];
    > char sgcaa[] = "hello!";
    > //cab = "xyz";
    > cab[0] = 'x';
    > cac = "xyz";
    >
    > gca = gcaa;
    > //sgca = sgcaa;
    > // gcaa = gca;
    >
    > px = &main;


    px is a pointer to int.
    main is a function (void) returning int.
    &main is a pointer to function (void) returning int.
    &main and px have incompatible types.
    The assignment is a constraint violation.

    > }


    --
    Simon.
     
    Simon Biber, Jul 25, 2006
    #5
  6. Richard Bos

    Simon Biber Guest

    wrote:
    > Richard Bos wrote:
    >> "" <> wrote:
    >>
    >>> I have the following source code. It seems wierd to me why gca's value
    >>> cannot be reassigned. It's afterall a pointer and has a pointer value.
    >>> char gca[7];

    >> That's not a pointer.
    >>
    >> No, it's not even a "constant pointer".
    >>
    >> RTFFAQFFS: <http://c-faq.com/aryptr/index.html>.
    >>
    >> Richard

    >
    > Arrays are ``second-class citizens'' in C; one upshot of this prejudice
    > is that you cannot assign to them.


    Indeed.

    > Well, I guess it's just reiterating my confusion. Why should arrays be
    > second class citizens? gca does have a memory location. The value of
    > gca has to be stored in memory so gca[0] etc can be accessed.


    Arrays are second class citizens because that's the way they are defined.

    gca does have a memory location, in the same way that an int variable
    has a memory location. The int variable's memory location is known to
    the compiler, but it is not stored in memory.

    You are attempting to do the equivalent of this:

    int foo;
    int bar;

    &foo = &bar; /* constraint violation */

    Why can't you assign to the address of foo? Foo has a (fixed) memory
    location, but &foo does not have its own memory location. It's just a
    value that the compiler calculated for you.

    Similarly, the array gca has a memory location but its location is not
    stored in memory; it is fixed. You cannot change its address.

    --
    Simon.
     
    Simon Biber, Jul 25, 2006
    #6
  7. Richard Bos

    Guest

    Simon Biber wrote:
    > wrote:
    > > Richard Bos wrote:
    > >> "" <> wrote:
    > >>
    > >>> I have the following source code. It seems wierd to me why gca's value
    > >>> cannot be reassigned. It's afterall a pointer and has a pointer value.
    > >>> char gca[7];
    > >> That's not a pointer.
    > >>
    > >> No, it's not even a "constant pointer".
    > >>
    > >> RTFFAQFFS: <http://c-faq.com/aryptr/index.html>.
    > >>
    > >> Richard

    > >
    > > Arrays are ``second-class citizens'' in C; one upshot of this prejudice
    > > is that you cannot assign to them.

    >
    > Indeed.
    >
    > > Well, I guess it's just reiterating my confusion. Why should arrays be
    > > second class citizens? gca does have a memory location. The value of
    > > gca has to be stored in memory so gca[0] etc can be accessed.

    >
    > Arrays are second class citizens because that's the way they are defined.
    >
    > gca does have a memory location, in the same way that an int variable
    > has a memory location. The int variable's memory location is known to
    > the compiler, but it is not stored in memory.
    >
    > You are attempting to do the equivalent of this:
    >
    > int foo;
    > int bar;
    >
    > &foo = &bar; /* constraint violation */
    >
    > Why can't you assign to the address of foo? Foo has a (fixed) memory
    > location, but &foo does not have its own memory location. It's just a
    > value that the compiler calculated for you.
    >
    > Similarly, the array gca has a memory location but its location is not
    > stored in memory; it is fixed. You cannot change its address.
    >
    > --
    > Simon.


    Thanks, you've made it very clear now. The key is the value of gca as a
    pointer is a compiler calculated value that has no memory storage.
    That's why it cannot be reassigned.

    Fei
     
    , Jul 25, 2006
    #7
  8. "" <> writes:
    [...]
    >> > Richard Bos wrote:
    >> >> "" <> wrote:
    >> >>
    >> >>> I have the following source code. It seems wierd to me why gca's value
    >> >>> cannot be reassigned. It's afterall a pointer and has a pointer value.
    >> >>> char gca[7];
    >> >> That's not a pointer.

    [...]
    > Thanks, you've made it very clear now. The key is the value of gca as a
    > pointer is a compiler calculated value that has no memory storage.
    > That's why it cannot be reassigned.


    Right.

    Just to pound on the point a bit harder, gca is an array object, not a
    pointer. Arrays are not pointers, and pointers are not arrays.

    What causes the confusion is that, in most contexts, an expression of
    array type is implicitly converted to a pointer to its first element.
    The resulting pointer is a value, not an object. It might be clearer
    to say that the array expression is converted to *the address of* its
    first element. (This conversion does not occur if the array
    expression is the operand of a unary "&" or "sizeof" operator, or if
    it's a string literal used in an initializer for an array.)

    And, as you know by now, this is well explained in section 6 of the
    FAQ, <http://www.c-faq.com/>.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 25, 2006
    #8
  9. On 25 Jul 2006 07:31:43 -0700, in comp.lang.c , ""
    <> wrote:

    >I have the following source code. It seems wierd to me why gca's value
    >cannot be reassigned. It's afterall a pointer and has a pointer value.


    NO. Pointers are not arrays, and arrays are not pointers.
    Arrays are blocks of memory.
    Pointers are addresses of memory.

    A house is not an address, or vice versa.

    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Jul 25, 2006
    #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. Brian Stubblefield

    incompatible types in assignment

    Brian Stubblefield, May 25, 2004, in forum: C Programming
    Replies:
    7
    Views:
    33,664
    Michael Fyles
    May 30, 2004
  3. v4vijayakumar

    incompatible types in assignment

    v4vijayakumar, Jun 5, 2006, in forum: C Programming
    Replies:
    16
    Views:
    909
    Keith Thompson
    Jun 6, 2006
  4. Replies:
    3
    Views:
    819
  5. dannydb368

    HELP please - incompatible types in assignment

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

Share This Page