Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
gcc数组中的值不一致_C_Arrays_Gcc - Fatal编程技术网

gcc数组中的值不一致

gcc数组中的值不一致,c,arrays,gcc,C,Arrays,Gcc,我在浏览其他问题,但找不到与我的问题相关的任何问题。基本上,我是在写一个代码来读取一些包含数据的文本文件,然后再进行处理。但我面临一些奇怪的问题,数组中的值不断变化。下面我给出了两个“for”循环,其中一个是从文件指针读取值,并立即使用printf语句进行检查。下一个“for”循环是我再次检查存储的值,由于某些原因,它与一些随机值不同 for (int i = 0; i < ndihedrals; ++i) { fscanf (rea

我在浏览其他问题,但找不到与我的问题相关的任何问题。基本上,我是在写一个代码来读取一些包含数据的文本文件,然后再进行处理。但我面临一些奇怪的问题,数组中的值不断变化。下面我给出了两个“for”循环,其中一个是从文件指针读取值,并立即使用printf语句进行检查。下一个“for”循环是我再次检查存储的值,由于某些原因,它与一些随机值不同

        for (int i = 0; i < ndihedrals; ++i)
        {
            fscanf (readdihedral, "%d %f %d %d %d %d\n", &sino_dih[i], &angle_dih[i], &atom1_dih[i], &atom2_dih[i], &atom3_dih[i], &atom4_dih[i]);
            printf("i: %d ==> %d %f %d %d %d %d\n", i, sino_dih[i], angle_dih[i], atom1_dih[i], atom2_dih[i], atom3_dih[i], atom4_dih[i]);
        }

        for (int i = 0; i < ndihedrals; ++i)
        {
            printf("test2: i: %d ==> %d %f %d %d %d %d\n", i, sino_dih[i], angle_dih[i], atom1_dih[i], atom2_dih[i], atom3_dih[i], atom4_dih[i]);
        }
以下是第二个for循环的输出:

test2: i: 0 ==> 1 0.000000 4533 4440 4441 4443
test2: i: 1 ==> 2 0.000000 4534 4442 4441 4443
test2: i: 2 ==> 3 0.000000 4535 4441 4443 4444
test2: i: 3 ==> 4 0.000000 4536 4441 4443 4444
test2: i: 4 ==> 5 0.000000 4537 632 633 635
test2: i: 5 ==> 6 0.000000 4538 634 633 635
test2: i: 6 ==> 7 0.000000 4539 633 635 636
test2: i: 7 ==> 8 0.000000 4540 633 635 636
test2: i: 8 ==> 9 0.000000 4541 635 636 638
test2: i: 9 ==> 10 0.000000 4542 637 636 638
test2: i: 10 ==> 11 0.000000 4543 636 638 639
这是代码的声明部分,我用malloc和calloc尝试了代码

        sino_dih = (int *) calloc (natoms, sizeof (int));
        atom1_dih = (int *) calloc (natoms, sizeof (int));
        atom2_dih = (int *) calloc (natoms, sizeof (int));
        atom3_dih = (int *) calloc (natoms, sizeof (int));
        atom4_dih = (int *) calloc (natoms, sizeof (int));
        angle_dih = (float *) calloc (natoms, sizeof (float));
我在这里附上了完整的代码(链接:)


数组的大小是
natoms
,但您正在访问元素
0
nIDEDRALS-1
。如果
natoms
将存在未定义的行为。 我想你应该分配
nDiderals
许多元素,例如

sino_dih = calloc(ndihedrals, sizeof(int));
atom1_dih = ...
...

(我必须阅读完整的代码才能得出结论)

你必须先检查
fscanf()
的返回值,然后才能相信它“读取”的内容。发布代码链接没有帮助,请发布一个链接。或者至少是数组的声明和定义。我在第一个for循环中使用printf语句检查了这些值,这些值是正确的。或者你指的是其他方法?很难知道,请发布数组的声明。您是否使用了
malloc()
?您还可以使用。您使用了
natoms
,然后使用了
nDiderals
,为什么?你为什么不使用
malloc()
而不是
calloc()
,有什么特别的原因吗?也。请发布所有能够重现您的问题的信息。因为如果不是这样的话,要帮你就很难了。非常感谢。你有时间阅读全部代码。此外,您还可以删除
calloc()
中的强制转换,间接表示不需要强制转换,还可以改进多余的空格,使代码更干净,还可以间接表示有更好、更可读的方式来编写代码。您是对的。我在写答案时没有注意到你的评论,抱歉:(好的,我不知道你为什么感到抱歉。你回答的问题是正确的。
My GCC version: 7.3.1 20180312
sino_dih = calloc(ndihedrals, sizeof(int));
atom1_dih = ...
...