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.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Yes, GCC will treat it as 'const char *'. MSVC has some settings to treat string literals as constant too IIRC (and store them in read-only memory). Are you sure 'char *' is the default setting there ? Would be a bit surprising, but MSVC has always been a sort of black box to me concerning some of its settings.
Quick aside - the biggest reason that this is code above is a bad technique is that it is actually functionally different from:
The reason being this code following:
char *p = func1();
*p = 'A';
Not only will code using the static variable not segfault, but it'll allow you to reassign the value of empty. At least on modern systems with modern gcc, you should get a segfault due to writing to a read-only codespace. Both aren't good, but one can lead to "dangerous things" where the other (hopefully) just crashes.
I agree - both are bad, but I at least made a mention of it not being preferable, while you gave it as a solution to the OP. Apart from that, your code will still spawn this warning mentioned in the first post ...
And "dangerous things" ??? Oh, please ... *facepalm*
Another question, if I do "", does gcc treat it as string or char * ? I know in vc++, it treats "" as char *.
From the C++ standard 2.13.4 String Literal
A string literal is a sequence of characters (as defined in 2.13.2) surrounded by double quotes, optionally beginning with the letter L, as in "..." or L"...". A string literal that does not begin with L is an ordinary string literal, also referred to as a narrow string literal. An ordinary string literal has type “array of n const char” and static storage duration, where n is the size of the string as defined below, and is initialized with the given characters. A string literal that begins with L, such as L"asdf", is a wide string literal. A wide string literal has type “array of n const wchar_t” and has static storage duration, where n is the size of the string as defined below, and is initialized with the given characters.
Or put another way anything in double quotes should be const char *.
If you plan to return a sting literal then the signature of your function should reflect that the return value is a const. If you are returning a string that can be modified but you want to be able to return an empty string return 0, the null pointer, and then test that the return value points to a valid address before doing anything with the value.
so what is the final answer if I really need to return char * instead of const char * ?
Returning 0 or NULL would be the best action so that you do not try and modify the string pointed to but you can use the same way as you are even though the quote by graemef states it is constant. As IIRC a string literal will deform to a char* for C compatibility reasons. This behaviour is depreciated yet not disallowed.
Last edited by dmail; 08-28-2009 at 07:43 AM.
Reason: added depreciated comment