c - 为什么我的命令窗口在传递标签时显示总线错误?

标签 c

我目前正在做一个学校项目,我们需要做一些排序,但是在我的代码中,我在运行 double 而不是单精度时遇到了总线错误

#include <stdio.h>
#include <string.h>

void singleprecision()
{
    float x[21];
    x[1] = 11.0f/2.0f; x[2] = 61.0f/11.0f;

    int k = 3;
    for (; k <= 10; k++)
    {
        x[k] = 111.0f-(1130.0f - 3000.0f/x[k-2])/x[k-1];
        printf("x[%d]:%f\n",k,x[k]);
    }
}



void doubleprecision()
{
    double x[21];
    x[1]=11.0/2.0;
    x[2] = 61.0/11.0;

    int k = 3;
    for(; k<=20; k++);
    {
        x[k] = 111.0 - (1130.0 - 30000.0/x[k-2])/x[k-1];
        printf("x[%d]:%lf\n",k,x[k]);
    }
}


int main(int argc, char *argv[])
{ 
    int i = 0; // will there be double precision used based on the g input in the command line

    if (argc == 2 && strcmp(argv[1],"-d")==0) // checking for -g input on the command line
    {
        doubleprecision();
        printf("Double Precision is being used \n");
    }
    else 
    {
       printf("Single Precision is being used \n");
       singleprecision();
    }

   return 0;
}

任何帮助都可以帮助我克服这个挑战并理解它。

我在远程 linux 系统上运行并使用 gcc 进行编译,它编译得很好,但在 double 时同样会失败。

最佳答案

在函数中:doubleprecision()删除无关的;从语句末尾开始:for(; k<=20; k++)然后代码运行没有问题。

关于c - 为什么我的命令窗口在传递标签时显示总线错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52938764/

相关文章:

c++ - 在 Delphi 2010 中使用 C/C++ DLL

c - 使用 C 在终端中打印多字节字符

c - 来自加速度计的数据转换

c - 默认参数提升

c - 执行 while 循环的问题

c - 使用文件 io 打印整行

c - 指向数组的指针

c - 用 C 直接读写压缩文件

c++ - 是否可以强制Linux在释放后使虚拟内存失效

c - WIN32 : Obtain Graphics Card Information?