Do string constants persist out of scope?
Wm E. Davidsen Jr
davidsen at sixhub.UUCP
Tue Jan 16 12:56:13 AEST 1990
In article <1380 at mit-amt.MEDIA.MIT.EDU> adamk at media-lab.media.mit.edu.UUCP (Adam Kao) writes:
|
| If I define a string constant in a procedure and then pass it back
| out, does the pointer I get remain valid? I'm confused because
| strings are really char pointers.
The answer to your question is yes, a string constant is permanently
allocated. However, a string is not "really a char pointer" the way you
mean it, the quote notation denotes a permanently allocated character
array, null terminated, having an rvalue of the address of the first
character. I don't have a standard here, but that's close.
One of the major differences between K&R and ANSI is that strings are
constants. In K&R you could do some bizarre things by modifying the
value of strings. In ANSI that is either forbidden or not guaranteed to
work (I believe the latter) and the compiler is allowed to have one
address for all constants having the same value. In K&R it says all
strings must be separate arrays.
In the case:
foo("abc", "abc");
K&R requires there to be two strings, ANSI allows one. I don't believe
it requires only one, but you will have to look that up if you care.
--
bill davidsen - sysop *IX BBS and Public Access UNIX
davidsen at sixhub.uucp ...!uunet!crdgw1!sixhub!davidsen
"Getting old is bad, but it beats the hell out of the alternative" -anon
More information about the Comp.lang.c
mailing list