我有以下问题: 我正在填充一个2D数组,其行数是我正在读取的文件数。因此,每一行的列数对应于从文件读取的数据字节。
考虑到这张图片,我有以下程序: 我必须返回从每个文件读取的数据的字节数组和每个文件的大小。这就是我所拥有的:
void fillArrays(unsigned char **array, size_t dataSize[], int *nFiles)
{
printf("Calling fillArrays\n");
int i, j, nrows;
nrows = 3;
*nFiles = nrows; // assuming no. of files to be read = 3
dataSize = (size_t *)malloc(nrows * sizeof(size_t));
array = (unsigned char**)malloc(nrows * sizeof(unsigned char *));
dataSize[0] = 4; // assuming file 1 contains 4 bytes
dataSize[1] = 3; // assuming file 2 contains 3 bytes
dataSize[2] = 1;// assuming file 3 contains 1 bytes
//populating file data into a 2D array. Here for test purpose assuming each file has data = 0x03!!
for(i = 0; i < nrows; i++)
{
array[i] = (unsigned char *) malloc(dataSize[i] * sizeof(unsigned char));
for(j = 0; j < dataSize[i]; j++){
printf("round %d %d\n", i,j);
array[i][j] = 0x03;
printf("array [%d][%d] = %02X\n ", i, j , array[i][j]);
}
//array[i][dataSize[i]]= '\0';
}
}
int main(int argc, char *argv[])
{
unsigned char **fileArray;
int noFiles = 0;
size_t *fileSize;
fillArrays(fileArray,fileSize, &noFiles);
printf("Returned no. of files = %d\n", noFiles);
printf("fileSize[0] = %lu\n", fileSize[0]);
printf("fileSize[1] = %lu\n", fileSize[1]);
printf("fileSize[2] = %lu\n", fileSize[2]);
int j = 0;
for (int i = 0; i < noFiles; i++){
printf("i = %d\n", i);
for (int j = 0; j < fileSize[i]; j++){
printf("Obtained data from file %d : (fileArray [%d][%d]) = %02X\n ", i, i, j , fileArray[i][j]);
//j++;
}
}
return 0;
}
该代码在主函数的'j'循环中出现段错误。有人可以给点灯吗? 如何做得更好?
谢谢。
答案 0 :(得分:1)
您正在按值传递fileArray
,因此在fillArrays
中,您正在将该指针的副本复制为指针,因此在执行fillArrays
函数之后,main函数中的fileArray
是不受影响-它仍未初始化,您需要通过 reference 传递此变量(传递指向fileArray
的指针):
fillArrays(&fileArray,fileSize, &noFiles);
然后在fillArrays
中,您需要添加以下修改:
void fillArrays(unsigned char ***array, size_t dataSize[], int *nFiles)
{
//...
*array = (unsigned char**)malloc(nrows * sizeof(unsigned char *));
//...
(*array)[i] = (unsigned char *) malloc(dataSize[i] * sizeof(unsigned char));
///...
(*array)[i][j] = 0x03;
}
答案 1 :(得分:1)
您的array
和dataSize
在fillArrays
函数的本地,并且
在fillArrays
内为它们分配内存不会影响main
内的数组。
// Allocates the memory to local variables
dataSize = (size_t *)malloc(nrows * sizeof(size_t));
array = (unsigned char**)malloc(nrows * sizeof(unsigned char *));
因此,您需要使用指向char*
和fileArray
的指针
指向size_t*
的{{1}}的指针。
您的原型将如下所示。
fileSize
然后调用以下函数。
void fillArrays(unsigned char ***array, size_t **dataSize, int *nFiles)
示例代码:
fillArrays(&fileArray,&fileSize, &noFiles);
替代方法:
我建议您不要将指向void fillArrays(unsigned char ***array, size_t **dataSize, int *nFiles)
{
printf("Calling fillArrays\n");
int i, j, nrows;
nrows = 3;
*nFiles = nrows; // assuming no. of files to be read = 3
*dataSize = (size_t *)malloc(nrows * sizeof(size_t));
*array = (unsigned char**)malloc(nrows * sizeof(unsigned char *));
(*dataSize)[0] = 4; // assuming file 1 contains 4 bytes
(*dataSize)[1] = 3; // assuming file 2 contains 3 bytes
(*dataSize)[2] = 1;// assuming file 3 contains 1 bytes
//populating file data into a 2D array. Here for test purpose assuming each file has data = 0x03!!
for(i = 0; i < nrows; i++)
{
(*array)[i] = (unsigned char *) malloc((*dataSize)[i] * sizeof(unsigned char));
for(j = 0; j < (*dataSize)[i]; j++){
printf("round %d %d\n", i,j);
(*array)[i][j] = 0x03;
printf("array [%d][%d] = %02X\n ", i, j , (*array)[i][j]);
}
//array[i][dataSize[i]]= '\0';
}
}
int main(int argc, char *argv[])
{
unsigned char **fileArray;
int noFiles = 0;
size_t *fileSize;
fillArrays(&fileArray,&fileSize, &noFiles);
printf("Returned no. of files = %d\n", noFiles);
printf("fileSize[0] = %lu\n", fileSize[0]);
printf("fileSize[1] = %lu\n", fileSize[1]);
printf("fileSize[2] = %lu\n", fileSize[2]);
int j = 0;
for (int i = 0; i < noFiles; i++){
printf("i = %d\n", i);
for (int j = 0; j < fileSize[i]; j++){
printf("Obtained data from file %d : (fileArray [%d][%d]) = %02X\n ", i, i, j , fileArray[i][j]);
//j++;
}
}
return 0;
}
的指针作为参数,而只需从char*
返回填充的数组,如下所示。
fillArrays
,然后按如下所示调用函数。
char** fillArrays(size_t **dataSize, int *nFiles)
{
printf("Calling fillArrays\n");
int i, j, nrows;
char **array = NULL;
nrows = 3;
*nFiles = nrows; // assuming no. of files to be read = 3
*dataSize = (size_t *)malloc(nrows * sizeof(size_t));
array = (unsigned char**)malloc(nrows * sizeof(unsigned char *));
(*dataSize)[0] = 4; // assuming file 1 contains 4 bytes
(*dataSize)[1] = 3; // assuming file 2 contains 3 bytes
(*dataSize)[2] = 1;// assuming file 3 contains 1 bytes
//populating file data into a 2D array. Here for test purpose assuming each file has data = 0x03!!
for(i = 0; i < nrows; i++)
{
array[i] = (unsigned char *) malloc((*dataSize)[i] * sizeof(unsigned char));
for(j = 0; j < (*dataSize)[i]; j++){
printf("round %d %d\n", i,j);
array[i][j] = 0x03;
printf("array [%d][%d] = %02X\n ", i, j , array[i][j]);
}
//array[i][dataSize[i]]= '\0';
}
return array;
}