I have two cases to return the string. One returns a pointer and other returns the char array. The first case works fine while printing the string, but I am getting a segmentation fault error on the second case. I want to know why this is happening?
This is the first case, and it gives the output.
char* voice(void){
return "hey!";
}
int main(){
printf ("%s\n", voice()); //output: hey!
return 0;
}
This is the second case, and it gives a segmentation fault.
char* voice(void){
char str[] = "hey!";
return str;
}
int main(){
printf ("%s\n", voice()); //segmentation fault
return 0;
}