Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > incompatible types in assignment

Reply
Thread Tools

incompatible types in assignment

 
 
v4vijayakumar
Guest
Posts: n/a
 
      06-05-2006
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
#

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      06-05-2006
v4vijayakumar said:

> 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


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
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
 
 
 
maurice.gray@gmail.com
Guest
Posts: n/a
 
      06-05-2006
Also.

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

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

return 0;

}

v4vijayakumar wrote:
> 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
> #


 
Reply With Quote
 
maurice.gray@gmail.com
Guest
Posts: n/a
 
      06-05-2006
#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;

}



v4vijayakumar wrote:
> 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
> #


 
Reply With Quote
 
Haider
Guest
Posts: n/a
 
      06-05-2006

v4vijayakumar wrote:
> 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
> #

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.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-05-2006
v4vijayakumar wrote:
>
> 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
> #


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

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

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.


 
Reply With Quote
 
Nelu
Guest
Posts: n/a
 
      06-05-2006
On 2006-06-05, Haider <(E-Mail Removed)> wrote:
>
> v4vijayakumar wrote:
>> <snip>
>> int ia1[10] = {0, 1, 2};
>> int ia2[10];
>> ia2 = ia1;
>> <snip>

> in place of int ia2[10]; use int *ia2.
> rest is fine.

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.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
 
Reply With Quote
 
whyglinux
Guest
Posts: n/a
 
      06-05-2006
v4vijayakumar wrote:
> 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
> #


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.

 
Reply With Quote
 
whyglinux
Guest
Posts: n/a
 
      06-05-2006
CBFalconer wrote:

> Arrays are not fundamental objects, and cannot be bodily assigned
> in C.


Pointers, structs and unions, as well as arrays, are all derived (not
fundamental) types, but they are assignable while arrays are not.

 
Reply With Quote
 
whyglinux
Guest
Posts: n/a
 
      06-05-2006

Haider wrote:

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


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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP please - incompatible types in assignment dannydb368 C Programming 0 03-15-2009 06:51 PM
"Incompatible types" error though types are same generic type wdh3rd@gmail.com Java 3 04-15-2007 02:36 PM
error: incompatible types in assignment Richard Bos C Programming 8 07-25-2006 10:20 PM
incompatible types in assignment Brian Stubblefield C Programming 7 05-30-2004 04:16 PM
Re: malloc & incompatible types in assignment Dimitris Mandalidis C Programming 4 08-31-2003 02:47 PM



Advertisments