ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Xconsole:
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[6]="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.
Quote:
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.
Code:
// 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[20];
// 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)
char* str3[20];
// This is a constant
// It is READ-ONLY
// You MUST NOT try to write to it!
char* str4 = "Hi.123456789";
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.