Not sure what you're really doing in your code, but if you're talking about creating an array inside of a function and trying to return it, here are a few words on the subject.
First of all, returning an array is basically just returning a pointer to the first element.
Right:
Code:
int *createArray(int iLength)
{
int *array = malloc(iLength * sizeof(*array)); /* Make sure it's dynamic memory,
NOT local to the function! */
return array;
}
malloc() has not been casted as a favor to those that freak out over it.
WRONG:
Code:
int *createArray(int iLength)
{
int array[iLength]; /* WRONG WRONG WRONG */
return array; /* WRONG WRONG WRONG */
}
The reason the last example is wrong is because local variables are destroyed upon the return of the function. In the first example, the memory was located from the heap while local variables are allocated on the stack.
Edit: Corrected mistake, missing sizeof(). My sincerest apologies.