我希望它根据文件中的值创建一个2D数组。我可以通过使用以下代码使数组成为本地来实现此目的:
int main()
{
int a = 5; //this is from a file
int b = 6; //this is from a file
int (*array2d)[a] = malloc(sizeof(int)*a*b);
return 0;
}
但是,我希望array2d是全球性的。以下不起作用:
int **array2d
int main()
{
int a = 5; //this is from a file
int b = 6; //this is from a file
(*array2d)[a] = malloc(sizeof(int)*a*b);
return 0;
}
有没有简单的方法可以做到这一点?
答案 0 :(得分:1)
可以使用函数对2D数组的内存进行分配和取消分配:
#include <stdlib.h>
#include <stdio.h>
int ** allocIntArray(int nrows, int ncols)
// allocate memory for 2D array and returns pointer if successful or NULL if failed
{
int r, c; // rows and cols counters
int ** parray; // pointer to array
// allocate memory for rows pointers
parray = (int **) malloc(nrows * sizeof(int *));
if( parray == NULL) // check
{
return NULL; // error sign
}
// allocate memory for each row
for (r = 0; r < nrows; r++)
{
parray[r] = (int*) malloc(ncols * sizeof(int));
if( parray[r] == NULL ) // check
{
// clean memory that was allocated before error
while(--r >= 0)
{
free(parray[r]);
}
free(parray);
return NULL; // error sign
}
}
// return when success
return parray;
}
int freeIntArray(int nrows, int **parray)
// frees memory allocated for 2D array and returns 1 if successful or 0 if failed
{
int r; // rows counter
if( parray == NULL || nrows < 1)
{
return 0;
}
// free memory allocated for each row
for (r = 0; r < nrows; r++)
{
if(parray[r] != NULL)
{
free(parray[r]);
}
}
// free memory allocated for rows pointers
free(parray);
return 1;
}
有了这两个函数,你可以创建任何大小的数组,例如:
int ** globArr;
int nrows; // number of rows
int ncols; // number of columns
int main(int argc, char * argv[])
{
// let's filename is in argv[1]
if( argc < 2) // we have not filename
{
printf("File name must be given as command line argument!\n");
return 1; // exit from program
}
// when we have filename try to use it
FILE * f = fopen(argv[1], "r");
if( f == NULL ) // we cannot read from file
{
printf("File %s cannot be read!\n", argv[1]);
return 2; // exit from program
}
// when we have file openned for reading
// we try to read first two numbers and use them as size of array
if( 2 != fscanf(f, "%d %d", &nrows, &ncols) ) // we cannot read two numbers
{
printf("ERROR: Wrong file format!\n");
return 3; // exit
}
// check that numbers are positive
if( nrows < 1 || ncols < 1 )
{
printf("ERROR: Wrong data size!\n");
return 4; // exit
}
// now we can allocate memory
globArr = allocIntArray(nrows, ncols);
// check that array allocated
if(globArr == NULL)
{
printf("ERROR: Cannot allocate memory!\n");
return 5; // exit
}
// and start a loop to read data from file
int r, c;
for(r = 0; r < nrows; r++)
{
for(c = 0; c < ncols; c++)
{
if (feof(f)) // end of file reached
{
printf("ERROR: Unexpected end of file!\n");
return 6; // exit
}
if( 1 != fscanf(f, "%d", &globArr[r][c]) )
{
printf("ERROR: Wrong file format!\n");
return 6; // exit
}
}
}
fclose(f);
// Now work with data
// . . .
return freeIntArray(nrows, globArr);
}
注意:文件格式可能与我的不同。我希望,该程序将以格式
读取文件2 3
10 20 30
40 50 60
或
2 3 10 20 30 40 50 60
或
2
3
10
20
30
40
50
60
其中2和3定义了2d-array的大小