Hello there,
I'm creating a program that reads numbers from a file and then creates a 2-D matrix with them. The first line of the file has the number of rows and columns of the matrix and the next lines contains ordered pairs.
So I wanted the user to be prompted to type the path of the file, and then pass that path to a function which would create, initialize and return the 2-D matrix. This matrix would be then passed to other functions.
After searching a little, I found out that C has restrictions to variable sizes arrays, so here's what I am doing:
The problem is that this way, in the return statement I get:Code:int * Create_Matrix (char * f_name){ int a, b; FILE * p_file = fopen(f_name, "rt"); int size; fscanf(p_file, "%d", &size); int matrix [size][size]; Initialize_Matrix(&matrix[0][0], size); return &matrix[0][0]; }
"Address of stack memory associated with local variable 'matrix' returned."
And if I change the matrix declaration to static int, I get:
"Variable length array declaration can not have 'static' storage duration."
So is there any way to make it work?
I appreciate any advice.



5Likes
LinkBack URL
About LinkBacks



