1

i have create a student structure and when i am assigning name to the character array defined inside the structure then it is giving me an error "incompatible pointer to integer conversion assigning char to char [13]....can anyone expalins me the reason why this is happening?

int main()
{
    typedef union {
        int roll_no;
        char name[30];
    } student;
    student student1;
    student1.roll_no = 5;
    student1.name[30] =
        "shivam kumar"; // this is line where it is giving me error
    printf("\n%d", student1.roll_no);
    printf("\n%s", student1.name);

    return 0;
}
artm
  • 17,291
  • 6
  • 38
  • 54

1 Answers1

1

In C you can't copy string that way:

    student1.name[30] =
        "shivam kumar"; // this is line where it is giving me error

Instead use strcpy:

student student1 = {0};
student1.roll_no = 5;
strcpy(student1.name, "shivam kumar");  //<--

It'd better to use strncpy to make sure the destination buffer is not overflow:

 strncpy(student1.name, "shivam kumar", sizeof(student1.name));
 student1.name[sizeof(student1.name) - 1] = '\0';   // make sure it's NUL-terminated.
artm
  • 17,291
  • 6
  • 38
  • 54
  • 1
    I'd also explain the need to check the length of the string before the copy to ensure there is no overrun. – David C. Rankin Aug 31 '20 at 06:27
  • @DavidC.Rankin agree, I added a note based on your comment. It's a bit of a pain to use strcpy indeed. – artm Aug 31 '20 at 06:34
  • Maybe it is better to mention `strncpy` instead, since unlike `strlcpy` it is a part of C standard? – r3mus n0x Aug 31 '20 at 06:41
  • @r3musn0x indeed – artm Aug 31 '20 at 06:46
  • @r3musn0x it's still a pain with `strncpy` - too much details – artm Aug 31 '20 at 06:51
  • 1
    Good deal, I hesitate to use `strncpy()`, not because it is wrong, just due to the `NOTES` section catching some new users by surprise when the string they thought was nul-terminated -- isn't `:)` See [man 3 strncpy](https://man7.org/linux/man-pages/man3/strncpy.3.html). It is fine for its purpose as long as the notes are kept in mind. I'd generally just do `size_t len = strlen(the_string);` and then `if (len >= sizeof student.name) { /* handle the error */ }` The use of a `union` instead of a `struct` is a bit bizarre as well -- that will be the next Q coming... – David C. Rankin Aug 31 '20 at 07:03
  • @DavidC.Rankin the part of ensuring `strncpy` to have proper `\NUL` terminated is annoying. And then the part of `strlcpy` not in the standard is annoying as well. There was some reason why they don't include `strlcpy` but it never really makes sense to me (and so I don't remember) – artm Aug 31 '20 at 07:09
  • here it is: https://stackoverflow.com/questions/2114896/why-are-strlcpy-and-strlcat-considered-insecure#:~:text=strncpy%20is%20a%20function%20that,%2Dterminated%20strings)%20at%20all.&text=strlcpy%20correctly%20does%20everything%20a,is%2C%20regretfully%2C%20not%20standard. – artm Aug 31 '20 at 07:12
  • C string is nightmare, too much details, too easy to make mistakes – artm Aug 31 '20 at 07:14
  • Or no, no -- C is the ultimate freedom, but with that freedom to use every bite comes the responsibility to use them with care. There is a reason projects that demand the ultimate efficiency are written in C, and I'm very thankful we have it -- image writing it all in assembler `:(` – David C. Rankin Aug 31 '20 at 08:26
  • `strncpy` is considered far more dangerous than `strcpy`. In this case you could as well be using `memcpy`, so use that one - much faster. – Lundin Aug 31 '20 at 12:48