I am trying to return an array of integers from a method into an array in my main. I know that you cannot pass an array in C, but you can pass a pointer to the array. My problem is though that every time I pass the pointer, I seem to get random numbers that are unrelated to the actual array back when magicsquaresize is less than/equal to 9. When it is less than 16, part of the array back is the actual array in main and when magicsquare is 25 or more, the whole array is actually brought back. I am really confused on why this would be. Here is my code:
//in my main
int* mptr = initSquare (magicsquaresize ) ;
printf("\n");
int i;
for (i = 0; i<magicsquaresize;i++) {
printf( "mptr[%d] = %d\n",i, *mptr);
//int initSquare, magicsquare is an array of ints that is def correct:
int *mptr = magicsquare;
for (i = 0; i<magicsquaresize;i++) {
printf( "mptr[%d] = %d\n",i,*(mptr+i));
}
return mptr;
Thz for any help anyone can give me.