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

Home Posts Topics Members FAQ

error: incompatible types in assignment

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 '06 #1
8 24235
"fe*****@gmail. com" <fe*****@gmail. comwrote:
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
Jul 25 '06 #2


fe*****@gmail.c om 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.

--
Er*********@sun .com

Jul 25 '06 #3

Richard Bos wrote:
"fe*****@gmail. com" <fe*****@gmail. comwrote:
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 '06 #4
fe*****@gmail.c om 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.
Jul 25 '06 #5
fe*****@gmail.c om wrote:
Richard Bos wrote:
>"fe*****@gmail .com" <fe*****@gmail. comwrote:
>>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.
Jul 25 '06 #6

Simon Biber wrote:
fe*****@gmail.c om wrote:
Richard Bos wrote:
"fe*****@gmail. com" <fe*****@gmail. comwrote:

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 '06 #7
"fe*****@gmail. com" <fe*****@gmail. comwrites:
[...]
Richard Bos wrote:
"fe*****@gmail .com" <fe*****@gmail. comwrote:

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_Keit h) 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.
Jul 25 '06 #8
On 25 Jul 2006 07:31:43 -0700, in comp.lang.c , "fe*****@gmail. com"
<fe*****@gmail. comwrote:
>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
Jul 25 '06 #9

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
4
1954
by: John Doe | last post by:
segmentation error !!!! hi guys , i wrote this program to multiply two matrices (just the basic code without checkin 4 the condition n==p ) " #include<stdio.h> main() { int a,b,c; int m,n,p,q,i,j,k;
13
11999
by: william | last post by:
code segment: long int * size; char entry; ............. size=&entry; ************************************* Gcc reported 'assignment of incompatible pointer type'.
4
2233
by: lovecreatesbea... | last post by:
Gcc only gives out a warning: `assignment discards qualifiers from pointer target type' against code such as following: $ type a.c int main(void) { const char *pc; char *p = pc;
10
2342
by: MisterE | last post by:
typedef struct sg { int a; } G; int c(G* g) { return g->a; }
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;
6
1740
by: fishwater00 | last post by:
Question 1: typedef struct { int x, y, z ; float *wave; float *velocity; }Model; void a(Model *domain); void b(Model *domain, int t);
5
2539
by: happytoday | last post by:
I compiled that program under turbo C without any problems but with sunstudi I found those errors: "pnt02ma1.c", line 37: warning: implicit function declaration: system "pnt02ma1.c", line 58: operands must have arithmetic type: op "*" "pnt02ma1.c", line 58: assignment type mismatch: pointer to float "=" double "pnt02ma1.c", line 59: operands must have arithmetic type: op "/" "pnt02ma1.c", line 60: operands have incompatible types:...
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
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5555
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
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.