0

I am trying to assign a Character array in struct using a string literal as shown below but it is not working:

s1.name[20] = "Mark";//prints some garbage string with special symbols

But when I tried assigning it using strcpy its working fine. Could someone explain me why it is failing in the 1st case?

strcpy(s1.name, "Mark");//This Works

Below is the complete code:

struct student { 
char name[20];
int id;
float marks; 
};
int main(int argc, const char * argv[])
{
struct student s1;
s1.name[20] = "Mark";
//strcpy(s1.name, "Sonoo Jaiswal");
s1.id = 22;
s1.marks = 76;
printf("%s: , %d: , %f: \n",s1.name, s1.id,s1.marks);
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
sandywho
  • 353
  • 1
  • 7
  • 16
  • Name is `char[20]`, so you must `strcpy (s1.name, "Mark");` you cannot assign strings to `char[]`. If `s1.name` was `char*`, you could assign the address for the *String Literal* `"Mark"`, but you cannot do that with an *array*. (if it was C++ and `s1.name` was a `std::string`, then assignment would be OK) – David C. Rankin Jun 16 '19 at 20:39
  • 1
    Don't add C++, this is C. – Hatted Rooster Jun 16 '19 at 20:39
  • at the array position 20 you are assigning a value, which is actually out of bounds, because the array only can accessed at index 0 - 19, with 'strcpy(s1.name, "Mark")' you copy the string into the array, therefore it works – Stephan Schlecht Jun 16 '19 at 20:40
  • in ' s1.name[20] = "Mark";' the `"Mark"` is actually a char pointer, this is converted to int to char, but as mentioned above it is past the end of the array anyway – Stephan Schlecht Jun 16 '19 at 20:44
  • 2
    Read the compiler warnings – klutt Jun 16 '19 at 20:45
  • 1
    It says "assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast" – klutt Jun 16 '19 at 20:46
  • Hi @StephanSchlecht when I do char str[20] = "Hello World"; and try to print, it works fine. can you explain why this works and why not when used with struct? – sandywho Jun 16 '19 at 20:48
  • In the statement `s1.name[20] = "Mark";` the left hand side refers to a single element of `st.name` with index 20 (which lies outside of the bounds of the array). In general, i you have an array `arr`, then `arr[i]` refers to the `i`th element of that array, starting at 0. If you wanted to assign to the string one character at a time, then you could use explicit indexing to do it. E.g. `s1.name[0] = 'M';`, `s1.name[1] = 'a';`, ..., `s1.name[4] = '\0';` That would have the same effect as `strcpy`. – Tom Karzes Jun 16 '19 at 20:51
  • 1
    `char str[20] = "Hello World";` defines an array variable containing 20 chars and it is initially assigned the value '"Hello World"'. But in your code you are accessing an existing already defined array at the index 20 and try to assign a value to this position in the array. – Stephan Schlecht Jun 16 '19 at 20:54
  • @StephanSchlecht So the only way I can assign a string value to a char array in struct is by strcpy(or strncpy) or by individually assigning each array element and terminating it with Null character('\0')? – sandywho Jun 16 '19 at 20:56
  • 1
    `char str[20] = "Hello World";` declares an array and initializes it with a string literal. You are allowed to do that because the C language has different rules for initialization than it does for assignment. A declaration followed by an assignment `char str[20]; str[20] = "Hello World";` is not the same as a declaration with an initializer. In fact, that assignment is not legal. – user3386109 Jun 16 '19 at 20:57
  • Yes, for example use strcpy or strncpy. – Stephan Schlecht Jun 16 '19 at 20:59
  • Thank you all for your help. – sandywho Jun 16 '19 at 21:00
  • 2
    You can initialize the string in the `struct` with an initializer. `struct student s1 = {"Mark", 22, 76};` – user3386109 Jun 16 '19 at 21:00
  • It's not valid C. See [“Pointer from integer/integer from pointer without a cast” issues](https://stackoverflow.com/questions/52186834/pointer-from-integer-integer-from-pointer-without-a-cast-issues) – Lundin Jun 17 '19 at 06:46

1 Answers1

1

This

s1.name[20] = "Mark";

is an assignment statement. The left side of the expression statememnt has type char while the right side of the statement has the type char *.

So the compiler should issue a warning that you are trying to assign an object of the type char * to an object of the type char (in this context the type of the string literal char[5] is implicitly converted to the type char *).

What you need to do is to copy the string literal "Mark" into the chracter array name.

To do this you should use the standard C fnction strcpy.

strcpy( s1.name, "Mark" );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335