Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!samsung!rex!ames!excelan!crdgw1!sixhub!davidsen From: davidsen@sixhub.UUCP (Wm E. Davidsen Jr) Newsgroups: comp.lang.c Subject: Re: Do string constants persist out of scope? Message-ID: <406@sixhub.UUCP> Date: 16 Jan 90 01:56:13 GMT References: <1380@mit-amt.MEDIA.MIT.EDU> Reply-To: davidsen@sixhub.UUCP (bill davidsen) Organization: *IX Public Access UNIX, Schenectady NY Lines: 28 In article <1380@mit-amt.MEDIA.MIT.EDU> adamk@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@sixhub.uucp ...!uunet!crdgw1!sixhub!davidsen "Getting old is bad, but it beats the hell out of the alternative" -anon