Well it looks like everyone told you the the same thing. Here is the deal, the function main is statically designed so that the os knows exactly how to start a process. Now, a function that main calls will dynamically build its memory usage. There is nothing wrong with doing this:
Code:
int myfunction(int size) {
char str[size];
//do other stuff
return 1; //or whatever this function wants to return
}
BUT it is not okay for you to return the address of this dynamically created array:
Code:
//WRONG
char *myfunction(int size) {
char str[size];
return str;
}
Since the returned string will be the address of a variable local to that function. Remember that the funcion will be deleting all of its local variables when it is done!
Now here is some other cool things that you can do along similar lines:
Code:
int myfunction(int xsize, int ysize) {
struct dynamic_struct {
int x[xsize];
int y[ysize];
} array;
//do stuff...
return sizeof(array);
}
Once again, if you want to have a function return a pointer to something then use malloc(). If the array isn't needed outside the array then you can do something like I just showed you.