Reputation: 141
#include <stdio.h>
char *getString()
{
char str[]="textprint";
}
int main()
{
printf("%c",getString());
return 0;
}
What is the reason for receiving this garbage value in the output?
Upvotes: 0
Views: 2855
Reputation: 3832
You may want to have a look at this:
#include <stdio.h>
char * getString(void)
{
const * str = "textprint test";
return str;
}
int main()
{
printf("%s\n", getString() );
return 0;
}
See demo here
I ran this snippet fine at codepad.org without the const keyword but according to this article it might be necessary to avoid a warning from the compiler.
The code creates a string literal which is stored in a read only location in memory and the pointer holds its address. When getString() executes, its return value is a pointer to that string literal.
According to the aforementioned article:
"The C and C++ standards say that string literals have static storage"
This suggests that the code of the OP is redeemable, modified as follows:
#include <stdio.h>
char * getString(void)
{
static char str[] = "textprint";
return str;
}
int main()
{
printf("\n%s",getString());
return 0;
}
See demo here
In this example, what gets created is an array such that each element contains a character comprising the string. The array would only exist within the scope of getString() except for the addition of keyword "static" which greatly extends the life of that array. As a result, when getString() executes in main() the return value is an address that points to the array kept intact in memory.
Note, getString() must contain a return statement in order for the funciton to evaluate as an argument for printf(). Also, without specifying the keyword static, the address would point to corrupt memory and hence the resulting garbage data.
Upvotes: 1
Reputation: 26703
gcc -Wall
gets you several warningsCode:
#include <stdio.h>
// version 2
// this is a change done after all other warnings were resolved,
// if you try it yourself, keep the version 1 (see below) to see the
// enlightening warning
char str[]="textprint";
// example for "localisation" strings
char strFr[] = "imprimertexte";
const char *getString(void) // better signature
// (with void as parameter list) for function, for good practice;
// the keyword "const" prevents changing the global string,
// which is a safety precaution
{
// version 1 char str[]="textprint";
// this version 1, after all the other warnings are resolved,
// gets the enlightening warning
// "function returns address of local variable",
// so this got moved somewhere else for version 2
// Returning a pointer to a global variable/text from a function
// is not obviously useful - at first glance.
// One reason could be the design to be able to return different
// pointers to different strings, depending on some condition.
// I can imagine for example localisation of the texts.
// That could look like
// if(localisation == English) {return str;}
// else {return strFr;}
return str; // to avoid warnings
// a) "unused variable"
// b) "control reaches end of non-void function"
}
int main(void) // better signature for main, good practice
{
printf("%c",*getString()); // change what to print, in order to avoid warning
// "format '%c' expects type 'int', but argument 2 has type 'char *' " ,
// by changing from "pointer to char" to
// "what a pointer to char points to", i.e. "char"
printf("\n"); // print a newline for better readable output
// Alternative, for printing the complete text:
printf("%s\n", getString());
// this attempts to change the global string
// with the "const" keyword at the function head this does not work
// it gets: "error: assignment of read-only location '*getString()'"
// *getString()='T';
return 0;
}
Upvotes: 3