What's wrong with this simple code?

F

fajar96te

When I tried to compile this:

// ==============
#include <stdio.h>
void main()
{
char k[4] ;
k = "abc" ;
}
// ==============

I got:
error: incompatible types when assigning to type 'char[4]' from type
'char *'

I'm using gcc on MinGW and Windows 7.


What's going on?

Thanks
 
B

Ben Pfaff

fajar96te said:
When I tried to compile this:

// ==============
#include <stdio.h>
void main()
{
char k[4] ;
k = "abc" ;
}
// ==============

I got:
error: incompatible types when assigning to type 'char[4]' from type
'char *'

You can't assign to an array.
 
B

BartC

fajar96te said:
When I tried to compile this:

// ==============
#include <stdio.h>
void main()
{
char k[4] ;
k = "abc" ;
}
// ==============

I got:
error: incompatible types when assigning to type 'char[4]' from type
'char *'

Try:

strcpy(k,"abc");

You might need to include string.h.
 
J

Jens Thoms Toerring

fajar96te said:
When I tried to compile this:
// ==============
#include <stdio.h>
void main()

make that

int main( void )

main() is supposed to return an int and either takes no argumemts
or two (and not an unspecified number, which is the meaning of
not specifying any arguments.
{
char k[4] ;
k = "abc" ;

'k' is an array of chars and you can't assign to an array as
a whole (that holds for all kinds of arrays, not just arrays
of chars). Moreover, what you have on the right hand of the
equal sign is not an array of chars but a pointer to a string
literal (i.e a pointer to a const array of chars), so you have
two things going wrong here - on the one hand you try to assign
to an array and then you have different tyoes on both sides of
teh assignment.

What you may have seen is somthing like

char k[4] = "abc";

The important difference is that this isn't an assignment but
an initialization of an array - and this can only be done while
the array is defined. And that you can use a string literal here
is some special case in C to make it easier to initialize
strigs, without this special case you'd have to write

char k[4] = { 'a', 'b', c', '\0' };

as you have in any other type of array initialization.

Compare this to an int array: an initialization like

int x[3] = { 1, 42, -17 };
int y[3];

is fine, but you can't do neither

x = { 1, 2, 5 };

nor

y = x;
Regards, Jens
 
J

James Kuyper

When I tried to compile this:

// ==============
#include <stdio.h>
void main()
{
char k[4] ;
k = "abc" ;
}
// ==============

I got:
error: incompatible types when assigning to type 'char[4]' from type
'char *'

I'm using gcc on MinGW and Windows 7.


What's going on?

Other people have answered your question fairly completely, but no one
seems to have mentioned one of the two main alternative ways of fixing
your code:

const char *k;
k = "abc";

The string literal "abc" calls for allocation of an unnamed array of
char with four elements; the string literal itself is automatically
converted to a char* pointer to the first element of that array. Despite
the fact that it's a char*, rather than a const char*, it's undefined
behavior if your program tries to write to that array. This is because
'const' wasn't added to the language until long to Therefore, it's safer
to store the pointer in a const char*.

If you need to modify the array after creating it, don't use this
method. Use

char k[] = "abc";

instead.
 
S

Sjouke Burry

Thank you guys!

This left me wonder... is there any design-reason behind this peculiar
grammar of C? i.e., that one cannot assign to an array? Will it hurt
if one can directly assign an array as easily as k = "abc"; as one
would an integer?

Start programming in Fortran.
 
B

BartC

fajar96te said:
Thank you guys!

This left me wonder... is there any design-reason behind this peculiar
grammar of C? i.e., that one cannot assign to an array? Will it hurt
if one can directly assign an array as easily as k = "abc"; as one
would an integer?

You're probably confusing two concepts: arrays and strings.

Fixed-bound arrays *could* have been treated as value-types like numeric
types, pointers and structs (so they are manipulated by value, passed to
functions by value, and so on).

But a decision was made in C not to allow value semantics for arrays;
instead you can only manipulate array pointers. It could have been possible
to have both value-arrays and array-pointers, but with extra syntax to
distinguish the two uses. However the loss isn't that great, because
whole-array operations are rare, passing by value is inefficient, while
working with arrays of different lengths is tricky because the length
becomes part of the type.

With strings, things get more complicated because as well as a fixed-bound
char array, you also have this variable length of string in each one. For
example, how do you implement this:

char a[10000],b[10000];

a[0]='X'; /* a contains "X"
a[1]=0;

b=a;

Do you copy 2 characters, or 10,000? It's not clear whether these char
arrays contain strings, or contain arbitrary data.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top