C: diff between char* a and char s[]
Hello,
int main(void) {
char s[] = "Hello"; // here the data will be stored in stack
char* ss = "Hello"; // Can u tell me where this data will be stored in process
// memory layout
ss[0] = 'a'; // gives segmentation fault, why
}
Thanks in advance
Madhu
Reg: C: diff between char* a and char s[]
Then what is the difference between the below two statements
const char* p = (char*) malloc(50);
p[0] = 'a'; // This will gives a compilation error
const char pp[] = "Hello";
pp[0] = 'a'; // This will gives a compilation error
char* s = "Hello";
s[0] = 'a'; // this gives a runtime error segmentation fault.
Thanks in advance,
Madhu
Diff between char * and char[]
char * means "character pointer". That is, it is the address of a character. It is a single "Field". Trying to refer to char CP like CP[] means you are trying to access an array called CP.
char *CP[4] defines an array of 4 character pointers. CP[0] is the first of those pointers.
char C[4] defines an array of four characters. C[0] is the first of those characters.
Pointers are usually implemented as a word (sizeof(int)). Characters as 8/16 bits.
Not sure why you get the compiler errors, what are they?
Cheers - VP