L
lithiumcat
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.
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.