+ 10
why it give me error???
#include<stdio.h> int main() { char ch[20]; ch="hello llo"; printf("%s",ch); return 0; }
16 odpowiedzi
+ 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;
}
+ 8
Ashutosh k. try this instead...
#include<stdio.h>
int main()
{
char *ch = "hello llo";
printf("%s",ch);
return 0;
}
+ 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
+ 6
Thanks to all of you ,specially Rasik Raikar
+ 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.
+ 4
message me please
+ 3
ok but i didn't understand
+ 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;
}
+ 3
#include<stdio.h>
int main()
{
char *ch = "hello llo";
printf("%s",ch);
return 0;
}
actually char pointer is point to the string
+ 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;
}
+ 2
Gen2oo
Thk u .yes true
+ 2
Can any one explain in detail
+ 2
#include<stdio.h>
#include <string.h>
int main()
{
char ch[20];
strcpy(ch, "hello lloe");
printf("%s",ch);
return 0;
}
+ 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.
+ 1
You should fix the line
you write----
char ch[20];
ch="hello llo";
Write it as---
char ch[20]="hello llo";
+ 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