Returning local string value from a function in C

Hi,

If I have a code like this, what are the potential problems do you see?

const char* const retString() {
    return "hello"; /* string literal */
}                       

My questions are:

a) Since the string literal which is already a constant read only data (cannot be modifiable), should I need to explicitly mention const in the return type of this function?
b) Also, what this "char* const", const pointer serves here? what happens if it is just normal pointer "char*"?
c) I guess, this version of returning the string literal is re-entrant. May I know how?
d) Can I use reference here in the return type?

Thanks,
Ibrahim

a) It's platform dependent. In most cases you need not, for example, linux, windows xp or latter. In DOS, a string literal may be modified.
b) The value of returned pointer(ie. the address of the pointer) can't be modified.
c) You just read, no write, so it is is re-entrant.
d) There's no reference type in C.

HP-UX, also.

Yes, you should explicitly mention const. Use const whenever and wherever you don't want the programmer messing with the data's contents. Use it liberally. It's a safety feature.

b) what he said. The pointer is unmodifiable. I don't know if it's useful to return that, but you can have variables declared as that.

c) They don't interfere with each other, therefore no race condition. If two run at the same time, who cares? They don't write anything, don't change anything, don't use anything, don't compete.

d) A pointer is a reference. In C++ you have the & type, which can be a bit dangerous since there's no straightforward way to errorcheck it...

Does't HP-UX put a string literal in readonly section of a executable file? I think this is more a complier thing than OS thing on HP-UX.

Maybe pointers are dangerous than references, because pointers are TYPE related and can be casted to pointer to any object while references are OBJECTS related. Right?

I'd heard people mention porting problems from HP-UX with code written with the assumption of writable strings.

More a compiler than OS thing anywhere, nothing stops a compiler from putting it in one kind of segment or another. Some let you choose whether strings are writable.

You can reference a non-object, like a structure, union, or atomic type.

But it's still a pointer and should be checked, but a hidden pointer that acts like a real variable. A pointer you can and should check if it's NULL before you use it, but a reference, you have to take the pointer of it to check, and people frequently don't bother.