why it give me error??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 10

why it give me error???

#include<stdio.h> int main() { char ch[20]; ch="hello llo"; printf("%s",ch); return 0; }

20th May 2020, 1:43 AM
Ashutosh k.
Ashutosh k. - avatar
16 Answers
+ 13
You have made mistake in initialisation of array. Try this #include<stdio.h> int main() { char ch[20]="hello llo"; printf("%s",ch); return 0; }
20th May 2020, 1:50 AM
Puthur Harthik
+ 8
Ashutosh k. try this instead... #include<stdio.h> int main() { char *ch = "hello llo"; printf("%s",ch); return 0; }
20th May 2020, 1:50 AM
BroFar
BroFar - avatar
+ 7
Ashutosh k. so whenever you declare array with some name, say char are[4]; you program will be allocated with 4 bytes of contiguous memory, and your array name itself acts as a pointer, but by default it's constant character pointer which means you can't reassign it. string("hello llo") you are dealing with is string literal, it will be stored in some memory Location, just like an array it will terminated by null character, char* h = "ghjh"; h[2] has value j just like an array; so here "hello llo" will return a pointer to this base memory where string is stored [g][h][j][h]['\0'], and it isn't stored in the array are[4]. so you're try to reassign to a constant character pointer,with some new memory Location which you are not allowed, you should read error message
20th May 2020, 1:58 AM
ishwar
ishwar - avatar
+ 6
Thanks to all of you ,specially Rasik Raikar
20th May 2020, 5:23 AM
Ashutosh k.
Ashutosh k. - avatar
+ 5
Rasik Raikar i believe this is first time i see someone using puts() for string literal, that is nice! Also, you maybe know this but the reader may not know. String pointed to is read-only string and cannot be modified like char[20] can. i always type const char *s for read-only strings so its more obvious that it cannot be changed. Then you also get a compiler warning if you do.
20th May 2020, 6:35 PM
Gen2oo
Gen2oo - avatar
+ 4
message me please
23rd May 2020, 3:19 AM
Ashutosh k.
Ashutosh k. - avatar
+ 3
ok but i didn't understand
20th May 2020, 1:58 AM
Ashutosh k.
Ashutosh k. - avatar
+ 3
#include<stdio.h> int main() { char ch[20]; ch="hello llo"; //syntax error printf("%s",ch); return 0; } The above code will show syntax error as size of array must be define while declaring a string It is correct for pointers because the string inside quotes is allocated from the compiler at compile time, so you can point to this memory address. #include <stdio.h> int main() { char *s; s = "hello llo"; puts(s); return 0; }
20th May 2020, 5:18 AM
Rasik Raikar
Rasik Raikar - avatar
+ 3
#include<stdio.h> int main() { char *ch = "hello llo"; printf("%s",ch); return 0; } actually char pointer is point to the string
22nd May 2020, 1:44 PM
KAMRUZZAMAN MD
KAMRUZZAMAN MD - avatar
+ 2
Here, your ch is of array type, and you are trying to use assignment in c with character array. In c, you can do this type of assignment at the declaration of array. Change this code to this and it will work... #include<stdio.h> int main() { char ch[20]="hello llo"; printf("%s",ch); return 0; }
20th May 2020, 1:58 AM
Kushang Shah
Kushang Shah - avatar
+ 2
Gen2oo Thk u .yes true
20th May 2020, 6:52 PM
Rasik Raikar
Rasik Raikar - avatar
+ 2
Can any one explain in detail
23rd May 2020, 3:16 AM
B123
+ 2
#include<stdio.h> #include <string.h> int main() { char ch[20]; strcpy(ch, "hello lloe"); printf("%s",ch); return 0; }
23rd May 2020, 7:58 PM
Cristian Daraban
Cristian Daraban - avatar
+ 2
Noman you are right that it is the base address to a block of memory. ch[5] will fit all characters in "hello" but leaves out null-byte at the end , it would make it a char array but an invalid string. I also think *p and ch[20] should be of type char (maybe it's a typo), but I get your point.
23rd May 2020, 9:05 PM
Gen2oo
Gen2oo - avatar
+ 1
You should fix the line you write---- char ch[20]; ch="hello llo"; Write it as--- char ch[20]="hello llo";
21st May 2020, 7:41 AM
Shweta Rao
Shweta Rao - avatar
+ 1
hey bro i think array contain contigious memory but when we create a memory we create a block and array name is like a pointer as we see int ch[20]; ch='h'; here we see ch store a char h but question is what does mean of writing a name of array is like a base address of array then we write ch is like a pointer it treat likely pointer but it is not a pointer as we see an example int *p,ch[5]; p="hello"; is valid ch="hello" is not valid because it is not pointer and it is not auto increment i hope u understand what eror is
23rd May 2020, 7:11 AM
Noman
Noman - avatar