Results 1 to 6 of 6
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 ...
- 10-12-2010 #1Just Joined!
- Join Date
- May 2009
- Posts
- 21
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
- 10-12-2010 #2
Its stored in read only memory.
Make mine Arch Linux
- 10-12-2010 #3Just Joined!
- Join Date
- May 2009
- Posts
- 21
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
- 10-12-2010 #4
Number one its three statements not two. If your curious about programming at this level you should get a good tutorial on the debugger and familiarize yourself with bin utilities like objdump.
The first two are suggestions to the compiler, to make the variables const. How the compiler achieves this const state is implementation dependent....I think.Last edited by gerard4143; 10-12-2010 at 12:31 PM.
Make mine Arch Linux
- 10-19-2010 #5Just Joined!
- Join Date
- Nov 2009
- Posts
- 53
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
- 10-20-2010 #6Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
You have defined 'p' as a const char*, and then you are trying to modify it. You either need to declare it as simply a char* (recommended), or you need to cast away the const on the assignment (not recommended - a violation of the intention indicated by const).
Same reason as before. You cannot modify a const variable once it has been initialized.const char pp[] = "Hello";
pp[0] = 'a'; // This will gives a compilation error
You have initialized 's' with a string literal, which unless you change your compilation options is in read-only memory. So, when you try to modify it, the application will segfault as you discovered. The compiler should have also issued a warning about assigning a const string literal to a non-const variable.char* s = "Hello";
s[0] = 'a'; // this gives a runtime error segmentation fault.Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
