0

What is missing in this code which causing error and how to resolve this error??

#include <stdio.h> int main() { char arr[10]; arr="word"; printf("%s",arr); return 0; }

25th Feb 2020, 5:39 PM
Arshia
Arshia - avatar
3 Réponses
+ 2
You can't assign a string literal to a char array that already exist. Either you do it directly: char arr[10]="word"; Or you use the strcmp function from string.h. Or you do it by hand, letter by letter with a loop, writing a 0/'\0' at the end.
25th Feb 2020, 5:44 PM
HonFu
HonFu - avatar
+ 1
Either use char are[10]="xyz" Or use strcpy(are[10], "xyx") with library of #include<string.h>
26th Feb 2020, 12:42 PM
Shubham Vilayatkar
Shubham Vilayatkar - avatar
0
Actually when you are writing arr="word", its logically incorrect. Actually arr denotes the address of the first element of your array and it does not represent your array. So you should write char arr[10]= "word", thus initializing at the time of declaration. This will work. Kudos..
28th Feb 2020, 8:44 AM
Aryaman Mishra 🇮🇳
Aryaman Mishra 🇮🇳 - avatar