gpt4 book ai didi

c - 将 strtof 值分配给 C 中的二维数组

转载 作者:行者123 更新时间:2023-11-30 15:50:58 25 4
gpt4 key购买 nike

我正在尝试将 CSV 文件解析为 C 中的二维数组。我想制作一个具有结构的矩阵:

typedef struct {
int row;
int col;
float **arr;
int numElements;
} Matrix;

我正在使用的函数接受动态分配的二维 float 组,并返回。我正在使用 fgets 读取值,使用 strtok 对逗号之间的每个值进行标记,然后使用 strtof 转换返回的字符串。

首先,我动态创建了二维数组,然后将它们传递给函数以填充值,

RMatrix->arr = make2DArray(RMatrix->row, RMatrix->col);
VMatrix->arr = make2DArray(VMatrix->row, VMatrix->col);

printf("RMatrix->arr : %p \n", RMatrix->arr);
printf("VMatrix->arr : %p \n", VMatrix->arr);

parseCSV(fpRMatrix, RMatrix->arr, RMatrix->row, RMatrix->col, &(RMatrix->numElements), INPUT_LENGTH);
printf("RMatrix parsed\n");
parseCSV(fpVMatrix, VMatrix->arr, VMatrix->row, VMatrix->col, &(VMatrix->numElements), INPUT_LENGTH);
printf("VMatrix parsed\n");

以下是功能:

void parseCSV(FILE *fp, float **output, int row, int col, int *numElements ,int inputLength)
{
char *buffer;
int rowArr = 0;

printf("Output : %p \n", output);

buffer = (char*) malloc(inputLength * sizeof(char));

while(fgets(buffer, inputLength, fp)) {
char *p =strtok(buffer,",");
int colArr = 0;
float check = 0;

while(p)
{
printf("p now : %s \n", p);
check = strtof(p, (char**) NULL);
printf("check now : %f \n", check);
output[rowArr][colArr] = strtof(p, (char**) NULL);
*numElements += 1;
colArr++;
p = strtok('\0',",");
printf("output[%d][%d] : %f ", rowArr, colArr, output[rowArr][colArr]);
}
printf("\n");
rowArr++;
}
printf("numElements in the end : %d\n", *numElements);
free(buffer);

}

float **make2DArray(int row, int col)
{
float** arr;
float* temp;

arr = (float**)malloc(row * sizeof(float*));
temp = (float*)malloc(row * col * sizeof(float));
for (int i = 0; i < row; i++) {
arr[i] = temp + (i * row);
}

return arr;

}

输出:

Name : RMatrix
NumElements : 0
Rows : 2
Cols : 4
Name : VMatrix
NumElements : 0
Rows : 2
Cols : 4
RMatrix->arr : 0x11684d0
VMatrix->arr : 0x1168520
Output : 0x11684d0
p now : 1
check now : 1.000000
output[0][1] : 0.000000 p now : 2
check now : 2.000000
output[0][2] : 0.000000 p now : 3
check now : 3.000000
output[0][3] : 0.000000 p now : 4

check now : 4.000000
output[0][4] : 0.000000
p now : 5
check now : 5.000000
output[1][1] : 4.000000 p now : 6
check now : 6.000000
output[1][2] : 0.000000 p now : 7
check now : 7.000000
output[1][3] : 0.000000 p now : 8

check now : 8.000000
output[1][4] : 0.000000
numElements in the end : 8
RMatrix parsed
Output : 0x1168520
p now : 1
check now : 1.000000
output[0][1] : 0.000000 p now : 2
check now : 2.000000
output[0][2] : 0.000000 p now : 3
check now : 3.000000
output[0][3] : 0.000000 p now : 4

check now : 4.000000
output[0][4] : 0.000000
p now : 5
check now : 5.000000
output[1][1] : 4.000000 p now : 6
check now : 6.000000
output[1][2] : 0.000000 p now : 7
check now : 7.000000
output[1][3] : 0.000000 p now : 8

check now : 8.000000
output[1][4] : 0.000000
numElements in the end : 8
VMatrix parsed

如您所见,strtof 调用成功(反射(reflect)在 p 和检查变量中),但未对数组进行赋值。

我才使用 C 一个月,但我已经被它迷住了。然而,很明显我需要了解更多。我真的很感谢你的帮助:)

最佳答案

这个

arr[i] = temp + (i * row);

应该是

arr[i] = temp + (i * col);

因为i = [0,row-1]

关于c - 将 strtof 值分配给 C 中的二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15465016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com