user7425291
user7425291

Reputation:

Why return statement given error in C?

What is the difference between char s[] and char *s?

I have given an example of two codes based on the given link. Assuming getstring() a function.


 char *str = "GfG"; /* "GfG" is stored in read only part of shared segment */
 /* str has auto storage duration,so stored on the stack
 /* No problem: remains at address str after getString() returns*/
 return str; 

AND

char str[] = "GfG"; /* "GfG" is stored in read only part of shared segment */ 
/* str has auto storage duration,so stored on the stack.
/* Problem: string may not be present after getSting() returns */
return str; 

As both have automatic storage duration and both variables stored on the stack segment.

Then, why return str works for the first one and not for the second one ?

Upvotes: 1

Views: 97

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 311048

In the first code snippet the variable str declared like

char *str = "GfG"; 

is indeed a local variable of the function with the automatic storage duration that will not alive after exiting the function.

However the string literal itself that the variable points to has the static storage duration and its life does not depend on a function call. It will be alive after exiting the function. So the function returns pointer to the first character of the string literal.

In the second case there is declared a local array

char str[] = "GfG"; 

that is initialized by characters of the string literal. After exiting the function it will not be alive anymore. So a pointer to the first character of the array will be invalid.

You could achieve the same effect with the character array as with the string literal in the first code snippet if to declare it like

static char str[] = "GfG"; 

In this case the function may return pointer to the first character of the array because it will be alive after exiting the function due to its static storage duration.

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409356

String literals have a life-time of the whole programs execution, they never go out of scope. Therefore pointers to them will always work.

As you noted, the array in your second example have automatic storage and will go out of scope once the function returns, the pointer you return is not valid.


And a small note about a comment in your array example:

char str[] = "GfG"; /* "GfG" is stored in read only part of shared segment */ 

The comment above is wrong. When you initialize an array like that the compiler will automatically set up the array, there is no string literal in this case.

The definition and initialization of the array above is equivalent to

char str[4] = { 'G', 'f', 'G', '\0' };

I hope the comment is the same for the array example as for the pointer example because of bad copy-pasting?

Upvotes: 2

Both versions of str have automatic storage duration. But their types are different and that makes all the difference.

char *str - is a pointer. It points to a string literal. The storage duration of the literal is static. Returning str, will return the address of the literal (the contents of str are returned), all's well.

char str[] - is a local array. It is initialized from the literal. But the duration of the entire buffer is automatic. When you return it, it decays to a pointer (the address of str is returned). The buffer goes out of scope, so that address results in a dangling pointer.

Upvotes: 4

Related Questions