R
Robert Smith
I am wondering why it is possible to return a pointer to a string literal
(ie. 1) but not an array that has been explicitly allocated. (ie. 2) ?
Both would be allocated on the stack, why does the first one not cause a
compiler warning?
#include <stdio.h>
char * funca() {
char *a = "blah"; //1 - ok
// char a[] = "blah"; //2 - not ok
return a;
}
int main() {
funca();
return 0;
}
(ie. 1) but not an array that has been explicitly allocated. (ie. 2) ?
Both would be allocated on the stack, why does the first one not cause a
compiler warning?
#include <stdio.h>
char * funca() {
char *a = "blah"; //1 - ok
// char a[] = "blah"; //2 - not ok
return a;
}
int main() {
funca();
return 0;
}