gpt4 book ai didi

c - ISO C90 禁止混合声明和代码 sscanf

转载 作者:太空宇宙 更新时间:2023-11-04 00:16:53 27 4
gpt4 key购买 nike

我在尝试编译我的单元测试代码时遇到了一个奇怪的错误。出于某种原因,编译器将我的 sscanf 调用视为混合声明?我不太明白,这是整个错误:

cc1: warnings being treated as errors
/home/brlcad/brlcad/src/libbn/tests/bn_complex.c: In function 'main':
/home/brlcad/brlcad/src/libbn/tests/bn_complex.c:53: error: ISO C90 forbids mixed declarations and code
make[2]: *** [src/libbn/tests/CMakeFiles/tester_bn_complex.dir/bn_complex.c.o] Error 1
make[1]: *** [src/libbn/tests/CMakeFiles/tester_bn_complex.dir/all] Error 2
make: *** [all] Error 2

int
main(int argc, char *argv[])
{
double expRe1, expIm2, expSqRe1, expSqIm2;
double actRe1, actIm2, actSqRe1, actSqIm2;
actRe1 = actIm2 = actSqRe1 = actSqIm2 =
expRe1 = expIm2 = expSqRe1 = expSqIm2 = 0.0;
bn_complex_t com1,com2; //a struct that holds two doubles
if(argc < 5)
bu_exit(1, "ERROR: Invalid parameters[%s]\n", argv[0]);

sscanf(argv[1], "%lf,%lf", &com1.re, &com1.im); /* Error is HERE */
sscanf(argv[2], "%lf,%lf", &com2.re, &com2.im);
sscanf(argv[3], "%lf,%lf", &expRe1, &expIm2);
sscanf(argv[4], "%lf,%lf", &expSqRe1, &expSqIm2);

test_div(com1, com2, &actRe1, &actIm2);
test_sqrt(com1,com2, &actSqRe1, &actSqIm2);

if((fabs(actRe1 - expRe1) < 0.00001) || (fabs(actIm2 - expIm2) < 0.00001)){
printf("Division failed...\n");
return 1;
}
if((fabs(actSqRe1 - expSqRe1) < 0.00001) || (fabs(actSqIm2 - expSqIm2) < 0.00001)){
printf("Square roots failed...\n");
return 1;
}
return 0;
}

最佳答案

在 C90 中,所有声明都必须位于 block 的开头。放这个:

bn_complex_t com1,com2; //a struct that holds two doubles

在这些声明之上:

actRe1 = actIm2 = actSqRe1 = actSqIm2 =
expRe1 = expIm2 = expSqRe1 = expSqIm2 = 0.0;

关于c - ISO C90 禁止混合声明和代码 sscanf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13887288/

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