C中主要下标值出错

时间:2016-02-13 19:00:49

标签: c pointers function-pointers

我是C的新手,我正在尝试从某些值(px和py)计算动能,但我的例程总是指责为主要错误下标值既不是数组也不是指针向量也不是。任何人都可以帮助我,因为我知道我已经尝试过了。

动力学函数中出现错误

谢谢

我的日常工作

#include <stdio.h>
#include <stdlib.h>

#ifndef RAN2_H_
#define RAN2_H_

void UniformBox(long, long *, double, double, double *, double *, double *, double *);

void kinetic(long, double, double, double); 

#endif /* RAN2_H_ */

#include "ran2.h"

float ran2(long*);

void UniformBox(long n, long *idum, double L, double p0, double *rx, double *ry, double *px, double *py){


    long i;

    for(i = 0 ; i < n ; i++){
        rx[i] = ((double)ran2(idum))*L;
        ry[i] = ((double)ran2(idum))*L;
        px[i] = ((double)ran2(idum) -.5)*2*p0;
        py[i] = ((double)ran2(idum) -.5)*2*p0;
    }



    return;
}


void kinetic(long n, double x, double y, double kint){

    long i;

    for(i = 0 ; i < n ; i++){

        kint[i] = (x*x)+(y*y);
    }

    return;
 }


void UniformBox(long, long *, double, double, double *, double *, double *, double *);

void kinetic(long n, double x, double y, double kint);


int main(){

    long i, n, seed, idum;
    double *rx, *ry, *px, *py;
    double L, p0, kint;

    n = 1000;
    L = 2.0;
    p0 = 1.22;
    seed = 10;
    idum = -seed;

    FILE *init = fopen("initialPosition.dat", "w");

    rx = (double *) malloc((double) n * sizeof(double));
    ry = (double *) malloc((double) n * sizeof(double));
    px = (double *) malloc((double) n * sizeof(double));
    py = (double *) malloc((double) n * sizeof(double));

    UniformBox(n, &idum, L, p0, rx, ry, px, py);
    kinetic(n, px, py, kint);
    for(i = 0 ; i < n ; i++){
        printf("%lf\t%lf\t%lf\t%lf\t%lf\n", rx[i], ry[i], px[i], py[i], kint[i]);
        fprintf(init,"%lf\t%lf\t%lf\t%lf\n", rx[i], ry[i], px[i], py[i]);
    }

    fclose(init);

    free(rx);
    free(ry);
    free(px);
    free(py);
    return 0;
}

2 个答案:

答案 0 :(得分:0)

在C中,必须通过引用将数组传递给函数。因此,您的函数头应该是

void kinetic(long n, double x, double y, double * kint);

您也可以使用评论中建议的[]表示法。

答案 1 :(得分:0)

我修复了编译错误。有几点需要注意。

我删除了多个不兼容的函数原型。

除了需要成为指针的kinetic kint之外,xy需要成为指针。请注意,根据代码的总体情况,这是我的猜想。另请注意循环内的更改xx[i]。否则,事情对我来说没有意义。

main中,我为malloc

添加了kint

无论如何,这是代码[请原谅无偿的风格清理]:

#include <stdio.h>
#include <stdlib.h>

#ifndef RAN2_H_
#define RAN2_H_

float ran2(long *);

#endif /* RAN2_H_ */

//#include "ran2.h"

void
UniformBox(long n, long *idum, double L, double p0, double *rx, double *ry,
    double *px, double *py)
{

    long i;

    for (i = 0; i < n; i++) {
        rx[i] = ((double) ran2(idum)) * L;
        ry[i] = ((double) ran2(idum)) * L;
        px[i] = ((double) ran2(idum) - .5) * 2 * p0;
        py[i] = ((double) ran2(idum) - .5) * 2 * p0;
    }

    return;
}

void
kinetic(long n, double *x, double *y, double *kint)
{

    long i;

    for (i = 0; i < n; i++) {
        kint[i] = (x[i] * x[i]) + (y[i] * y[i]);
    }

    return;
}

int
main()
{

    long i, n;
    long seed, idum;
    double *rx, *ry;
    double *px, *py;
    double L, p0;
    double *kint;

    n = 1000;
    L = 2.0;
    p0 = 1.22;
    seed = 10;
    idum = -seed;

    FILE *init = fopen("initialPosition.dat", "w");

    rx = (double *) malloc((double) n * sizeof(double));
    ry = (double *) malloc((double) n * sizeof(double));
    px = (double *) malloc((double) n * sizeof(double));
    py = (double *) malloc((double) n * sizeof(double));
    kint = (double *) malloc((double) n * sizeof(double));

    UniformBox(n, &idum, L, p0, rx, ry, px, py);
    kinetic(n, px, py, kint);

    for (i = 0; i < n; i++) {
        printf("%lf\t%lf\t%lf\t%lf\t%lf\n", rx[i], ry[i], px[i], py[i], kint[i]);
        fprintf(init, "%lf\t%lf\t%lf\t%lf\n", rx[i], ry[i], px[i], py[i]);
    }

    fclose(init);

    free(rx);
    free(ry);
    free(px);
    free(py);

    return 0;
}