ossau
ossau

Reputation: 61

string literal in c

Why is the following code illegal?

typedef struct{
   char a[6];
} point;

int main()
{
   point p;
   p.a = "onetwo";
}

Does it have anything to do with the size of the literal? or is it just illegal to assign a string literal to a char array after it's declared?

Upvotes: 6

Views: 12750

Answers (6)

user411313
user411313

Reputation: 3990

No strcpy or C99 compund literal is needed. The example in pure ANSI C:

typedef struct{
   char a[6];
} point;

int main()
{
   point p;
   *(point*)p.a = *(point*)"onetwo";
   fwrite(p.a,6,1,stdout);fflush(stdout);
   return 0;
}

Upvotes: 1

dreamlax
dreamlax

Reputation: 95335

As other answers have already pointed out, you can only initialise a character array with a string literal, you cannot assign a string literal to a character array. However, structs (even those that contain character arrays) are another kettle of fish.

I would not recommend doing this in an actual program, but this demonstrates that although arrays types cannot be assigned to, structs containing array types can be.

typedef struct
{
    char value[100];
} string;

int main()
{
    string a = {"hello"};
    a = (string){"another string!"}; // overwrite value with a new string
    puts(a.value);

    string b = {"a NEW string"};
    b = a; // override with the value of another "string" struct
    puts(b.value); // prints "another string!" again
}

So, in your original example, the following code should compile fine:

typedef struct{
    char a[6];
} point;

int main()
{
   point p;

   // note that only 5 characters + 1 for '\0' will fit in a char[6] array.
   p = (point){"onetw"};
}

Upvotes: 3

Prasoon Saurav
Prasoon Saurav

Reputation: 92854

Arrays are non modifiable lvalues. So you cannot assign to them. Left side of assignment operator must be an modifiable lvalue.

However you can initialize an array when it is defined.

For example :

 char a[] = "Hello World" ;// this is legal
 char a[]={'H','e','l','l','o',' ','W','o','r','l','d','\0'};//this is also legal

 //but

 char a[20];
 a = "Hello World" ;// is illegal 

However you can use strncpy(a, "Hello World",20);

Upvotes: 7

ysap
ysap

Reputation: 8115

Note that in order to store the string "onetwo" in your array, it has to be of length [7] and not as written in the question. The extra character is for storing the '\0' terminator.

Upvotes: 1

Amarghosh
Amarghosh

Reputation: 59451

It doesn't have anything to do with the size. You cannot assign a string literal to a char array after its been created - you can use it only at the time of definition.

When you do

char a[] = "something";

it creates an array of enough size (including the terminating null) and copies the string to the array. It is not a good practice to specify the array size when you initialize it with a string literal - you might not account for the null character.

When you do

char a[10];
a = "something";

you're trying to assign to the address of the array, which is illegal.

EDIT: as mentioned in other answers, you can do a strcpy/strncpy, but make sure that the array is initialized with the required length.

strcpy(p.a, "12345");//give space for the \0

Upvotes: 13

Michael Mrozek
Michael Mrozek

Reputation: 175345

You can never assign to arrays after they've been created; this is equally illegal:

int foo[4];
int bar[4];
foo = bar;

You need to use pointers, or assign to an index of the array; this is legal:

p.a[0] = 'o';

If you want to leave it an array in the struct, you can use a function like strcpy:

strncpy(p.a, "onetwo", 6);

(note that the char array needs to be big enough to hold the nul-terminator too, so you probably want to make it char a[7] and change the last argument to strncpy to 7)

Upvotes: 7

Related Questions