ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you trying something like the first example above? If so, the answer to your question is that the string is effectively a constant in this case. But most of the times you can write to a char*, i.e. when there is a memory allocated to it with malloc(), of when it is declared like "char str". In that case "str" is also a char*, but you can write to the string through the pointer "str".
The reason is because pointers point to memory, and char *str points to memory in the data segment which is read only. The difference between char *str="hello" and str="hello" is that while both are declared the same way on the data segment, str is treated as readable because it is pushed onto the stack if it is used. *str and str are not declared any different by gcc when it creates an executable, the difference is in their use by the programmer. If you want to see what I mean, do a "gcc file.c -S" and view the .s file that is created, it will show you what GCC actually created from your .C
It will. char * is a variable. If you make it point to something you can't change, you can't change. In this case after making it point to a memory space(new char) which can be written to, you made a second statement that made it point to another "constant", which can't be changed. So you get a segmentation fault.
There ARE subtle (and important!) differences. And I know somebody's going to yell at me for "over-simplifying" here.
But honestly, for your purposes, you CAN regard pointers and arrays as "pretty much the same thing". At least for the time being.
And, if you want to explore the issue in more depth, then you should Google for "C programming array pointer". Or, better, read Kernighan and Ritchie.
Q: ok, i verified this but no result
this gives segmentation fault.
A: As Hko replied, that's EXACTLY the point he was trying to make!
See the example below.
// This is allocated from the heap with the C++ "new" operator
// It's read-write
// You must use "delete" to dispose of it
char* str1 = new char;
// This is allocated from the heap with the C "malloc()" function
// It's also read-write
// You must use "free()" to dispose of it
char* str2 = malloc (20);
// This is allocated from the stack
// It, too, is read-write
// It's disposed of when then block exits (usually, at the end of the function)
// This is a constant
// It is READ-ONLY
// You MUST NOT try to write to it!
char* str4 = "Hi.123456789";