gpt4 book ai didi

c - C 中的双数组打印输出给了我奇怪的输出

转载 作者:行者123 更新时间:2023-11-30 21:30:56 24 4
gpt4 key购买 nike

     a = (float**)malloc(num * sizeof(float*));
if( !a) {
printf("Cannot allocate a!\n");
exit(1);
}

for(i = 0; i < num; i++) {
a[i] = (float *)malloc(num * sizeof(float));
if( !a[i]) {
printf("Cannot allocate a[%d]!\n",i);
exit(1);
}}

for(i = 0; i < num; i++) {
for(j = 0; j < num; j++)
fscanf(fp,"%f ",&a[i][j]);
}


// in a method called after a is initialized and elements are recorded.
int s, t;
printf("This is num: %d\n", num);
for (s = 0; s < num; s++) {

for (t = 0; t < num; t++) {
printf("This is a[s][t]:%d for s:%d and t:%d\n", a[s][t], s, t);
}
}

This is num: 3 This is a[s][t]:0 for s:0 and t:4198765 This is a[s][t]:0 for s:1 and t:4199061 This is a[s][t]:0 for s:2 and t:4199061 This is a[s][t]:1 for s:0 and t:4199061 This is a[s][t]:1 for s:1 and t:4199061 This is a[s][t]:1 for s:2 and t:4199061 This is a[s][t]:2 for s:0 and t:4199061 This is a[s][t]:2 for s:1 and t:4199061 This is a[s][t]:2 for s:2 and t:4199061

//这是矩阵输入5 1 3 63 7 2 83 6 9 6

//顺序不对!

最佳答案

您应该使用 %f 来打印 float ,并检查每个 fscanf 是否成功或失败。

关于c - C 中的双数组打印输出给了我奇怪的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22431366/

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