c - 段错误 11 - C

标签 c segmentation-fault fault

我不明白为什么这段代码返回段错误。谁能帮我告诉我出了什么问题吗?

#include <stdio.h>
#include "rk78.h"
#include "flux.h"
#include "rtbps.h"

#define N 6
#define h0 0.1
#define pasmin 1E-6
#define pasmax 10
#define tol 1E-13

int rk78 (double *t, double x[], double *h,
          double hmin, double hmax, double tol,
          int n, int (*camp)(int n, double t, double x[], double f[], void *prm),
          void *prm);

int flux (double *t, double x[], double *h, double T,
          double pasmin, double pasmax, double tol, int npasmx,
          int n,
          int (*camp)(int n, double t, double x[], double f[], void *prm),
          void *prm);

int rtbps (int n, double t, double *x, double *f, void *prm);
/*
  Per compilar:
  gcc -o rtbps_int -g -Wall rtbps_int.c rtbps.c flux.c rk78.c  -lm
  Executar:
  ./rtbps_int 1.215058560962404e-2 < halos_inp.txt > halos.txt
*/

int main (int argc, char *argv[]) {
    double t,x[N],h,mu,T;
    int i, nt;
    if (argc!=2
        || sscanf(argv[1],"%lf",&mu)!=1
        ) {
        fprintf(stderr, "./rtbps_int mu\n");
        return -1;
    }

    /*
      posició, velocitat, temps, iteracions
    */
    printf("Ok");
    while(scanf("%lf %lf %lf %lf %lf %lf %lf %d", 
                &x[0],&x[1], &x[2], &x[3],&x[4],&x[5],&T,&nt)==8){

        printf("%lf %lf %lf %lf %lf %lf %lf\n", t, 
               x[0],x[1],x[2],x[3],x[4],x[5]);
        h=h0;
        t=0;
        for(i=1;i<=nt;i++){
            //t=0;
            flux(&t,x,&h,T/nt,pasmin,pasmax,tol,nt,N,rtbps,&mu);
            printf("%lf %lf %lf %lf %lf %lf %lf\n", t, 
                   x[0],x[1],x[2],x[3],x[4],x[5]);
        }
    }
    return 0;
}

在顶部,我复制了我使用的函数的原型(prototype),但它们可以工作,所以它们不是问题。

谢谢!

最佳答案

行中:

printf("%lf %lf %lf %lf %lf %lf %lf\n", t, 
    x[0],x[1],x[2],x[3],x[4],x[5]);

它看起来好像t 没有被初始化。不确定这是否是原因,但您可以尝试初始化它,看看是否遇到同样的问题。

double t=0.0,x[N],h,mu,T;

如果这不是原因,那么我实际上会开始怀疑您信任的功能!

关于c - 段错误 11 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42237301/

相关文章:

c - 如何读取 ebpf 中的堆栈跟踪内核端?

c - 如何在 C 中不使用指针的情况下通过函数传递 2D 数组

c - 初始化元素不是使用 C 的编译时常量

c - C 文件中的段错误

c - 具有 0 个命令行参数的简单程序段错误

c - segmentation fault C 有限状态机

c - Raspberry Pi RS232 读取 RFID 应答器

C++11:带有 std::thread 和 lambda 函数的段错误

c - 在c中分配给指向字符串的索引解引用指针时出现段错误

c - 为什么我在 while 循环中遇到这样的限制时会出现段错误?