1

Possible Duplicates:
string literal in c
Assigning strings to arrays of characters

I got the following code:

char result[3][4];

result[0] = "horse";
result[1] = "pig";
result[2] = "chicken";

It won't compile saying Incompatible types in assignment. What am I doing wrong?

Community
  • 1
  • 1
ryyst
  • 9,563
  • 18
  • 70
  • 97
  • Well those are char* and not char[4]... – leppie Jan 22 '11 at 18:38
  • @birryree: That question has its own possible duplicate http://stackoverflow.com/questions/579734/assigning-strings-to-arrays-of-characters –  Jan 22 '11 at 19:42

8 Answers8

4

char is a single char, eg 'h'

What you want is string pointers.

 char *result[3];

Remember if you do it this way you won't be able to change the contents of an individual string (it is a constant and stored in a different place.)

You could also do something like this

 char *result[3];

 result[0] =  malloc(100 * sizeof(char));
 strcpy(result[0],"horse"); // or strncpy(result[0],5,"horse")
 etc
Hogan
  • 69,564
  • 10
  • 76
  • 117
  • In OP's example `result` is a `char [3][4]`, and `result[i]` is thus a `char[4]`, not a `char`. – peoro Jan 22 '11 at 18:40
  • @peoro : true. Perhaps I could phrase the answer better. – Hogan Jan 22 '11 at 18:46
  • @user282635 : read this answer too, it might make things clearer. http://stackoverflow.com/questions/579734/assigning-strings-to-arrays-of-characters – Hogan Jan 22 '11 at 18:48
2
char result[3][10] = {
  "horse", 
  "pig", 
  "chicken",
}

or

char result[3][10];

strcpy_s(result[0], 10, "horse");
strcpy_s(result[1], 10, "pig");
strcpy_s(result[2], 10, "chicken");
Nick Banks
  • 4,298
  • 5
  • 39
  • 65
2

The following works better: char result[3][10] = {"horse", "pig", "chicken"};

Also, have in mind that "chicken" and "horse" have more that 4 characters and therefore result[3][4] (the [4] part) is not that right.

1

You probably wanted:

char* result[3];
result[0] = "horse";
result[1] = "pig";
result[2] = "chicken";

What you did was wrong for two reasons.

First, you can't "assign" a string to a char array like that, you'd have to use, for example, strcpy.

Second, your array dimension was also wrong, char array[4] obviously won't hold "horse" or "chicken", so it wouldn't work even if you corrected what I said above (it would compile, but you would be writing to memory you shouldn't be writing to).

What the corrected code does, is instead of storing the char array per se, it stores a pointer to the location in memory where "horse", etc., are stored. Therefore, it just stores an address, and you can then use the = operator.

houbysoft
  • 32,532
  • 24
  • 103
  • 156
1

Arrays are non-modifiable lvalues i.e you cannot assign to them.

You need to use array of pointers. Change char result[3][4]; to char * result[3]

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
1

This will not work because result declared as char result[3][4] and used as result[0] will result in something similar with: char * const result. Constant pointer doesn't admit any assignment, it already contains pointer to the array of columns "[4]".

Martin Babacaev
  • 6,240
  • 2
  • 19
  • 34
0

You can assign a string literal (eg: "horse") to a char array only when you declare it, not after.

char array[4] = "str"; // is OK
char array2[4];
array2 = "str"; // is NOT OK.

You could do like this:

char result[3][4] = { "horse", "pig", "chicken" };

but warning: "horse" and "chicken" are longer than char[4].


If you want to assign a string literal to an already declared char array you'll have to use strcpy (or other similar functions).


The other solution, suggested by many is to declare result as an array of char pointers: char *result[3];, but if you do like this you won't be allowed to modify the content of the strings pointed by result[i].

peoro
  • 25,562
  • 20
  • 98
  • 150
-1

You have to increase the size of the words and use strcpy():

char result[3][8];    // at least 8!

strcpy(result[0], "horse");
strcpy(result[1], "pig");
strcpy(result[2], "chicken");
BlackBear
  • 22,411
  • 10
  • 48
  • 86