I am trying to return a string from str function but it prints (null)
#include<stdio.h>
char* str()
{
char hi[10] = "return this";
return hi;
}
void main()
{
printf("%s", str());
}
I am trying to return a string from str function but it prints (null)
#include<stdio.h>
char* str()
{
char hi[10] = "return this";
return hi;
}
void main()
{
printf("%s", str());
}
In GCC at least your code compiles with teh following warnings:
main.c:12:19: warning: initializer-string for array of chars is too long
main.c:13:12: warning: function returns address of local variable [-Wreturn-local-addr]
Which more or less is a direct answer to your question. If you did not get these warnings, consider the compiler switched you are using, for GCC I suggest at least -Wall -Werror
- that will output the most useful warnings without being pedantic, and make those warnings errors so will prevent successful compilation until you fix them.
You are returning a pointer to a local-automatic variable that is no longer in scope after the function returns, so the result is undefined. You have also tried to initialise an array with more characters that you have reserved.
What the compiler has done here is given the invalid initialiser, it has set the address of hi
to null, and printf
has handles the null pointer by printing (null)
. That is behaviour specific to your compiler and C library - in other cases something different may happen. More insidious that that is that if your initialiser was not invalid (by being shorter), it is likely to have appeared to work and you might never have asked the question, but it would still be incorrect, and in more complex code would likely at some point cause observable erroneous behaviour.
In this particular case you could do any of the following:
const char* str()
{
static const char* hi = "return this";
return hi;
}
const char* str()
{
static const char hi[] = "return this";
return hi;
}
char* str( char* str, int maxlen )
{
str[maxlen] = '\0' ;
return strncpy( str, "return this", maxlen - 1 ) ;
}
void main()
{
char* buffer[32] ;
printf("%s", str(buffer, sizeof(buffer));
}
The most appropriate solution (and the above are by no means exhaustive) depend on what you actually want to do, since each solution differs semantically, and on its own this function is hardly practical. It would need a concrete real-world example to give best advice.
read static variables or global variables to access the variable outside the functions
there is an overflow in your code, read buffer overflow in c
Also read What should main() return in C and C++?
#include<stdio.h>
char* str()
{
static char hi[] = "return this";
return hi;
}
int main()
{
printf("%s", str());
return 0;
}
The problem is with the scope of the character array.
The solution for this issue is to make the address of that variable visible to the caller function!
i.e. one of the easiest solutions is use the below line in the declaration part of hi variable in your str() function. i.e. static char hi[10] = "return th";
in the declaration. This way you wouldn't need to change anything in this program BUT in the whole program, this variable WILL BE visible/accessible throughout the execution.
#include"stdio.h"
char* str() {
static char hi[10] = "return th";
return hi;
}
int main() {
printf("%s", str());
return 0;
}
#include<stdio.h>
#include<stdlib.h>
char* str(){
//malloc is used to allocate memory
char *hi=(char*)malloc(sizeof(char)*20);
char ch[]="return this\0";
for(int i=0;i<sizeof(ch);i++)
hi[i]=ch[i];
return hi;
}
int main(){
printf("%s", str());
return 0;
}