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.
Compilers are not guaranteed to store string literals in writeable memory. The reason your example is failing is because you're trying to modify read-only memory (the memory that s is pointing to).
This should not work since s converts to a 'char * const &' lvalue for the assignment which cannot be modified.
ta0kira
[EDIT]
RE perfect_circle: string literals generally use the same memory, but the address is not guaranteed past the expression it is used in. This means that although 'char *s="bla";' leaves s with a pointer to internally used memory, beyond that statement, the memory may not be there anymore. That is why you should copy string literals into static arrays with []. At the point where "bla" is used in the function call in the following line, the memory location for storing a string literal may be changed, making s become invalid. "bla" is loaded into memory just long enough for it to initialize something, such as a local variable or a static array; the pointer to this temporary location should never be used. Your second block is the equivalent of 'printf("%s\n","bla");', which is why it works because you do not have any new string literals before the function call.
ta0kira
[EDIT again]
RE itsme86: it isn't read-only memory; if it was, the pointer could not be converted to a char*; you would get a compiler error in that case. The pointer cannot be changed, which is what I think you mean (try doing 'char *&s = "bla";' in C++ to see what I mean). Or maybe you mean that the mem used for the literal does not belong to the user's process, thereby causing the segfault, just like trying to use a bad pointer, which also points to non-user mem?
ta0kira
There are other references at the bottom if you need them.
Code:
1.32: What is the difference between these initializations?
char a[] = "string literal";
char *p = "string literal";
My program crashes if I try to assign a new value to p[ i ].
A: A string literal can be used in two slightly different ways. As
an array initializer (as in the declaration of char a[]), it
specifies the initial values of the characters in that array.
Anywhere else, it turns into an unnamed, static array of
characters, which may be stored in read-only memory, which is
why you can't safely modify it. In an expression context, the
array is converted at once to a pointer, as usual (see section
6), so the second declaration initializes p to point to the
unnamed array's first element.
(For compiling old code, some compilers have a switch
controlling whether strings are writable or not.)
See also questions 1.31, 6.1, 6.2, and 6.8.
References: K&R2 Sec. 5.5 p. 104; ISO Sec. 6.1.4, Sec. 6.5.7;
Rationale Sec. 3.1.4; H&S Sec. 2.7.4 pp. 31-2.
Anyway, dereferencing a string literal through a pointer is not asking for trouble. It's perfectly valid. Just don't try to modify the string literal.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.