-2

I found this on geeksforgeeks

char *getString()
{
char *str = "Nice test for strings";
return str;
}

int main()
{
printf("%s", getString());
getchar();
return 0;
}

Output: “Nice test for strings”

I tried searching pointer functions but its not this. If you could point me to learn how this is done. It would be nice

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Joe Robin
  • 105
  • 2
  • 5
  • 3
    Pointer functions? Are you talking about `char *getString()`? Its a function returning a `char*`. – tkausl Apr 19 '21 at 11:22
  • "Pointer functions" ? That is not standard terminology. `getString` is a function and it **returns** a pointer. – MSalters Apr 19 '21 at 11:22
  • If you're asking to return pointers from functions, the pointer needs to be allocated dynamically. Or it needs to be static/global. Local pointers will be lost from the stack pointer when the function returns. – Irelia Apr 19 '21 at 11:28
  • @Nina *If you're asking to return pointers from functions, the pointer needs to be allocated dynamically. Or it needs to be static/global. Local pointers will be lost from the stack pointer when the function returns.* Given `char *str = "Nice test for strings";`, where do you think the pointer `str` points to? – Andrew Henle Apr 19 '21 at 11:31
  • @AndrewHenle It points to "Nice test for strings" but returning it doesn't garuntee the pointer will always point there. When other functions are called or other variables are allocated the stack pointer will shift around and that pointer will be lost.. – Irelia Apr 19 '21 at 11:32
  • @Nina The string constant isn't on the stack. Only `str` is on the stack, and `str`'s *value* is returned. You can safely return the *value* of a variable on the stack. Otherwise `int i = 3; return i;` wouldn't be safe. – David Schwartz Apr 19 '21 at 11:35
  • Oh I forgot literals are static. – Irelia Apr 19 '21 at 11:36
  • @Nina *It points to "Nice test for strings" but returning it doesn't garuntee the pointer will always point there....* Ouch. If I could downvote a comment, I would. `char *str = "Nice test for strings";` means `ptr` points to a string literal that's implicily `static`. The value contained in `str` is returned - **by value** so the address returned **by value** will always refer to the string literal. – Andrew Henle Apr 19 '21 at 11:37
  • It is unclear whether you are asking about functions that return pointers or about pointers to functions. You write “I tried searching pointer functions but its not this.” The pronouns in that sentence are unclear; the “it” of “its” could be the results of the search or “pointer functions” or something else, and the “this” could be the code you showed or “pointer functions” or something else. Rewrite your question to make clear what you are asking about. – Eric Postpischil Apr 19 '21 at 11:37
  • @AndrewHenle "The value contained in str is returned - by value" that is incorrect. A pointer to the value is still returned but because the string is static, then the data is contained. A pointer to the static string is still returned. – Irelia Apr 19 '21 at 11:48
  • https://www.geeksforgeeks.org/function-pointer-in-c/ – Stef1611 Apr 19 '21 at 11:49
  • @Nina "The value contained in str is returned - by value" is correct. The value of `str` is a pointer and it's returned by value – Stefan Riedel Apr 19 '21 at 12:21
  • @Nina: The value contained in `str` is an address. C 2018 6.2.5 20 says “… A pointer type describes an object whose value provides a reference to an entity of the referenced type.” The characters at that address are not contained in `str`. `return str;` returns the value of `str`. – Eric Postpischil Apr 19 '21 at 12:46
  • This was closed as a duplicate of a question about returning a pointer from a function, but I'm not certain that is what the OP is actually asking. It sounds to me as though he is asking about function pointers... If so, this is a better option for marking it as a duplicate: https://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work – Basya Apr 19 '21 at 18:28

1 Answers1

2

It seems you think that there is declared a pointer to a function

char *getString()

But this assumption is wrong.

There is declared the function getString that returns a pointer of the type char * to the string literal "Nice test for strings".

As string literals have static storage durations then the returned pointer will be valid after exiting the function.

A pointer to the function can look like

char * ( *getStringPtr )() = getString;

then using this pointer you could call the function like

printf("%s", getStringPtr());
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335