473,811 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Similar topics

7
593
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 following code during the compile stage: #include <stdio.h> #include <signal.h>
2
2193
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 this? the crypt function returns a pointer to a char. why an assignment to a char vector is not possible? MfG Dennis
10
4118
by: gk245 | last post by:
I have something like this: #include <stdio.h> main () { struct line { char write; char read;
8
24235
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 I cannot convince myself why this is prohibited. static int ia = {64, 64, 64, 64}; static char ca = {'a', 'b', 'c', 'd'}; static int iaa = {64, 64, 64, 64}; static char caa = {'a', 'b', 'c', 'd'}; static char cab = "abcd";
13
11999
by: william | last post by:
code segment: long int * size; char entry; ............. size=&entry; ************************************* Gcc reported 'assignment of incompatible pointer type'.
1
2876
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
2018
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 *)malloc(sizeof(struct node)); // give me memory such big p->id = pd;
4
2460
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 { char name; char value; } varlist; int main(int argc, char *argv) { varlist.name = "foo";
2
2766
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 rowIdx, idx = 0; int cRow = startX; for (idx = startX ; idx < height*width; idx += width) {
0
9730
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10392
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10403
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10136
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5693
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3868
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.