Wouldn't the pointer returned by the following function be inaccessible?
char *foo(int rc) { switch (rc) { case 1: return("one"); case 2: return("two"); default: return("whatever"); } }
So the lifetime of a local variable in C/C++ is practically only within the function, right? Which means, after char* foo(int)
terminates, the pointer it returns no longer means anything, right?
I'm a bit confused about the lifetime of a local variable. What is a good clarification?
The reason this string is 6 bytes long is that strings in C have a terminating NUL character to mark the end. That's not an array, it's a pointer to a string literal, and sizeof(str) will return the size of the pointer type for your machine.
Strings in C are arrays of char elements, so we can't really return a string - we must return a pointer to the first element of the string. All forms are perfectly valid. Note the use of const , because from the function I'm returning a string literal, a string defined in double quotes, which is a constant.
A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string.
A string literal is a sequence of zero or more characters enclosed within single quotation marks. The following are examples of string literals: 'Hello, world!' '10-NOV-91' 'He said, "Take it or leave it."'
Yes, lifetime of a local variable is within the scope({
,}
) in which it is created.
Local variables have automatic or local storage. Automatic because they are automatically destroyed once the scope within which they are created ends.
However, What you have here is an string literal, which is allocated in an implementation defined read-only memory. String literals are different from local variables and they remain alive throughout the program lifetime. They have static duration [Ref 1] lifetime.
A word of caution!
However, note that any attempt to modify the contents of an string literal is an undefined behavior (UB). User programs are not allowed to modify contents of a string literal.
Hence, it is always encouraged to use a const
while declaring a string literal.
const char*p = "string";
instead of,
char*p = "string";
In fact, in C++ it is deprecated to declare a string literal without the const
though not in C. However, declaring a string literal with a const
gives you the advantage that compilers would usually give you a warning in case you attempt to modify the string literal in second case.
Sample program:
#include<string.h> int main() { char *str1 = "string Literal"; const char *str2 = "string Literal"; char source[]="Sample string"; strcpy(str1,source); // No warning or error just Uundefined Behavior strcpy(str2,source); // Compiler issues a warning return 0; }
Output:
cc1: warnings being treated as errors
prog.c: In function ‘main’:
prog.c:9: error: passing argument 1 of ‘strcpy’ discards qualifiers from pointer target type
Notice the compiler warns for the second case, but not for the first.
To answer the question being asked by a couple of users here:
What is the deal with integral literals?
In other words, is the following code valid?
int *foo() { return &(2); }
The answer is, no this code is not valid. It is ill-formed and will give a compiler error.
Something like:
prog.c:3: error: lvalue required as unary ‘&’ operand
String literals are l-values, i.e: You can take the address of an string literal, but cannot change its contents.
However, any other literals (int
,float
,char
, etc.) are r-values (the C standard uses the term the value of an expression for these) and their address cannot be taken at all.
[Ref 1]C99 standard 6.4.5/5 "String Literals - Semantics":
In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals. The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence; for wide string literals, the array elements have type wchar_t, and are initialized with the sequence of wide characters...
It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With