Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > String literals...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 8 Topic 26087 of 26821
Post > Topic >>

String literals and storage duration

by lithiumcat@[EMAIL PROTECTED] May 6, 2008 at 05:41 AM

Hi,

I'm not yet very confident in my use of standard terminology, so
please be kind if I'm mis-calling something, I will do my best no to
make it again once pointed out.

I'm wondering what is the lifetime or a compile-time string constant,
I think that is what is called the storage duration of a string
litteral.

For example, let's say I've got a void foo(const char*) function, and
I'm calling it like this:
foo("simple string");
For how long can I use the pointer passed to foo? Let's say it stores
it somewhere, and another function, const char *bar(void) returns the
pointer passed to foo. Is that pointer still usable in a completely
different scope.

And I have the same questions with a call like this:
char *str = "simple string";
foo(str);

The last possibility I can think of is the following:
char str[] = "simple string";
foo(str);
That's the only one I think I know: str is an array with automatic
storage duration, and therefore the pointers that foo gets is valid as
long as we are in the scope of str. If str is declared like that in
the beginning of a function named "baz", a call to the previously said
bar() function would return a valid pointer until the function baz
exists. Or am I mistaken?


Thanks a lot for your help and your patience with me.
 




 8 Posts in Topic:
String literals and storage duration
lithiumcat@[EMAIL PROTECT  2008-05-06 05:41:30 
Re: String literals and storage duration
Richard Heathfield <rj  2008-05-06 12:48:47 
Re: String literals and storage duration
=?ISO-8859-1?Q?Tom=E1s_=D  2008-05-06 05:49:11 
Re: String literals and storage duration
Richard Heathfield <rj  2008-05-06 12:58:23 
Re: String literals and storage duration
lithiumcat@[EMAIL PROTECT  2008-05-06 08:39:07 
Re: String literals and storage duration
Chris Torek <nospam@[E  2008-05-08 19:38:52 
Re: String literals and storage duration
Robert Gamble <rgamble  2008-05-06 09:56:03 
Re: String literals and storage duration
Peter Nilsson <airia@[  2008-05-06 20:53:07 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Jul 9 0:55:18 CDT 2008.