473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

incompatible types in assignment

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
#

Jun 5 '06 #1
16 6346
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)
Jun 5 '06 #2
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
#


Jun 5 '06 #3
#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
#


Jun 5 '06 #4

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.

Jun 5 '06 #5
On 2006-06-05, Haider <hm*****@yahoo.com> 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...)
Jun 5 '06 #6
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.
Jun 5 '06 #7
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.

Jun 5 '06 #8
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.

Jun 5 '06 #9

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.

Jun 5 '06 #10
whyglinux wrote:
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.


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.
Jun 5 '06 #11
"CBFalconer" <cb********@yahoo.com> wrote in message
news:44***************@yahoo.com...
whyglinux wrote:
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.


Alright, if you insist, go ahead and make assignments to arrays. I
predict they won't work in C.


I think you misread "...while arrays are not."

- Bill
Jun 5 '06 #12
CBFalconer <cb********@yahoo.com> writes:
whyglinux wrote:
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.


Alright, if you insist, go ahead and make assignments to arrays. I
predict they won't work in C.


He knows that; he specifically acknowledged that arrays are not
assignable.
Note that a struct or union actually
defines a new type, an array only defines an aggregate of an old
type.


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 (The_Other_Keith) ks***@mib.org <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.
Jun 5 '06 #13
On 5 Jun 2006 09:00:33 -0700, "whyglinux" <wh*******@gmail.com> wrote
in comp.lang.c:
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.


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
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jun 6 '06 #14
Jack Klein wrote:

On 5 Jun 2006 09:00:33 -0700, "whyglinux" <wh*******@gmail.com> wrote
in comp.lang.c:
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.


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.


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 '06 #15
pete wrote:
Jack Klein wrote:

On 5 Jun 2006 09:00:33 -0700, "whyglinux" <wh*******@gmail.com> wrote
in comp.lang.c:
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.


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.


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.


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

[1]Emphasis mine. That's it -- I'd think.

Jun 6 '06 #16
Jack Klein <ja*******@spamcop.net> writes:
On 5 Jun 2006 09:00:33 -0700, "whyglinux" <wh*******@gmail.com> wrote
in comp.lang.c:
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.


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.


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 (The_Other_Keith) ks***@mib.org <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.
Jun 6 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Brian Stubblefield | last post by:
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...
2
by: Dennis Schulz | last post by:
Hi all, the following programm is supposed to check for login and password with inbuilt linux functions. in line 57 and 64 there are erorrs: incompatible types in asignment. whats wrong with...
10
by: gk245 | last post by:
I have something like this: #include <stdio.h> main () { struct line { char write; char read;
8
by: fei.liu | last post by:
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...
13
by: william | last post by:
code segment: long int * size; char entry; ............. size=&entry; ************************************* Gcc reported 'assignment of incompatible pointer type'.
1
by: pyo2004 | last post by:
Here's the definition of work_struct struct work_struct { unsigned long pending; struct list_head entry; void (*func)(void *); void *data; void *wq_data; struct timer_list timer; };
6
by: billiabon | last post by:
Hello,everybody! I need your help! My problem is this assignment char pnm; void enqueue(int pd,char pnm,int pnum,int ptm,struct queue *z) {struct node *p; p=(struct node...
4
by: pandaemonium | last post by:
I have two quick problems with the code I am writing: First, I define a struct of char arrays (strings) and then try accessing the same. I get an "incompatible types in assignment" error: struct...
2
by: kardon33 | last post by:
I am getting a "error: incompatible types in assignment" error and cant figure out why? I am trying to set lastRow to row at the end of the code snippet. int row; int lastRow; size_t...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.