如何在函数中用指针加两个值?

问题描述 投票:0回答:1

我的老师说,我必须使用复数类型将两个复数相加。我必须在一个函数中添加这些数字。我创建了一个类似的东西,但当我使用cppcheck

add.c:53:26: error: Uninitialized variable: a1 [uninitvar]
    double complex  z1 = a1 + b1*I;
                         ^
add.c:57:26: error: Uninitialized variable: a2 [uninitvar]
    double complex  z2 = a2 + b2*I;
                         ^
add.c:53:31: error: Uninitialized variable: b1 [uninitvar]
    double complex  z1 = a1 + b1*I;
                              ^
add.c:57:31: error: Uninitialized variable: b2 [uninitvar]
    double complex  z2 = a2 + b2*I;
                              ^
add.c:34:29: error: Uninitialized variable: c1 [uninitvar]
    double complex  csum1 = c1 + d1*I;
                            ^
add.c:34:34: error: Uninitialized variable: d1 [uninitvar]
    double complex  csum1 = c1 + d1*I;
                                 ^
add.c:52:15: error: Uninitialized variable: p [uninitvar]
    readZ(wz, p, q);
              ^
add.c:52:18: error: Uninitialized variable: q [uninitvar]
    readZ(wz, p, q);
                 ^
add.c:56:15: error: Uninitialized variable: r [uninitvar]
    readZ(wz, r, s);
              ^
add.c:56:18: error: Uninitialized variable: s [uninitvar]
    readZ(wz, r, s);
                 ^
add.c:59:18: error: Uninitialized variable: t [uninitvar]
    sumZ(p,q,r,s,t,u);
                 ^
add.c:59:20: error: Uninitialized variable: u [uninitvar]
    sumZ(p,q,r,s,t,u);

这是我的代码

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <complex.h>
#ifndef M_PI
    #define M_PI 3.14159265358979323846
#endif

void readZ(FILE *wp, double *a, double *b)
{
    char c;  
    assert(fscanf(wp,"%c%lg%c%lg%c%c",&c,a,&c,b,&c,&c));

}
int writeZ(FILE *wp, double complex z)
{    
    fprintf(wp, "%.2f, %.2f\n", creal(z), cimag(z));

    return 1;
}

void sumZ(double *a1, double *a2, double *b1, double *b2, double *c1, double *c2)
{
    *c1 = *a1 + *a2;
    *c2 = *b1 + *b2;
}

int main (int argc, char *argv[])
{    
    FILE *wz, *wc;  
    double a1, a2, b1, b2, c1, d1;
    double *p, *q, *r, *s, *t, *u;
    double complex  csum1 = c1 + d1*I;

    if (argc != 3) {                                
    printf("Wrong arguments number\n");
    printf("I should run this way:\n");
    printf("%s source result\n",argv[0]);
    exit(1);
    }

    if( (wz= fopen(argv[1],"r")) == NULL) {
        printf("Open error %s\n", argv[1]);
        exit(1);
    }
    if( (wc= fopen(argv[2], "w")) == NULL) {
        printf("Open error %s\n", argv[2]);
        exit(2);
    }

    readZ(wz, p, q);
    double complex  z1 = a1 + b1*I;
    writeZ(wc, z1);
    printf("%.2f, %.2f\n", creal(z1), cimag(z1));
    readZ(wz, r, s);
    double complex  z2 = a2 + b2*I;
    printf("%.2f, %.2f\n", creal(z2), cimag(z2));
    sumZ(p,q,r,s,t,u);
    printf("%.2f, %.2f\n", creal(csum1), cimag(csum1));

    return 0;
}

当我运行我的代码如add.x data.txt result.txt时,我得到了一个分段故障。他说我可以使用引用和指针。我现在不知道我应该怎么做。

c pointers complex-numbers
1个回答
1
投票

有一些错误。

有很多的 double * 指针变量[被单位化]。它们并不是真正需要的。main 可以只向下传递(例如) &a1&b1. 仅仅因为你被告知要使用指针,并不[一定]意味着你必须使用指针变量。

另外,指针作为函数的参数,只需要用于返回值。其他的可以直接使用 "通过值 "的变量。这就把事情简单化了一些。

我不得不根据对代码的分析来猜测正确的函数调用,以确定你的具体意图。

你的代码还是试图使用单独的 double 变量 a1b1 以获得 z1 而不是用[大部分]。double complex 变量无处不在。

此外,似乎在调用 sumZ 被颠倒了。


这是一个注解了错误和修复的版本。

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <complex.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

void
readZ(FILE *wp, double *a, double *b)
{
    char c;

#if 0
    assert(fscanf(wp, "%c%lg%c%lg%c%c", &c, a, &c, b, &c, &c));
#else
    assert(fscanf(wp, "%c%lg%c%lg%c%c", &c, a, &c, b, &c, &c));
#endif
}

int
writeZ(FILE *wp, double complex z)
{

    fprintf(wp, "%.2f, %.2f\n", creal(z), cimag(z));

    return 1;
}

// NOTE: not a bug exactly, but to simplify, b1/b2 and c1/c2 can be passed by
// value -- otherwise, mark them with "const" (e.g. const double *b1)
#if 0
void
sumZ(double *a1, double *a2, double *b1, double *b2, double *c1, double *c2)
{
    *c1 = *a1 + *a2;
    *c2 = *b1 + *b2;
}
#endif
#if 0
void
sumZ(double a1, double a2, double b1, double b2, double *c1, double *c2)
{
    *c1 = a1 + a2;
    *c2 = b1 + b2;
}
#endif
#if 1
void
sumZ(double complex a1, double complex b1, double complex *c1)
{
    *c1 = a1 + b1;
}
#endif

int
main(int argc, char *argv[])
{
    FILE *wz;
    FILE *wc;
    double a1;
    double a2;
    double b1;
    double b2;
// NOTE: with refactoring, these are now unused
#if 0
    double c1;
    double d1;
    double *p;
    double *q;
    double *r;
    double *s;
    double *t;
    double *u;
#endif
#if 0
    double csum1;
    double csum2;
#endif
// NOTE/BUG: c1 and d1 are _unitialized_
#if 0
    double complex csum1 = c1 + d1 * I;
#else
    double complex csum1;
#endif

    if (argc != 3) {
        printf("Wrong arguments number\n");
        printf("I should run this way:\n");
        printf("%s source result\n", argv[0]);
        exit(1);
    }

    if ((wz = fopen(argv[1], "r")) == NULL) {
        printf("Open error %s\n", argv[1]);
        exit(1);
    }
    if ((wc = fopen(argv[2], "w")) == NULL) {
        printf("Open error %s\n", argv[2]);
        exit(2);
    }

// NOTE/BUG: _pointers_ p and q are _unitialized_
#if 0
    readZ(wz, p, q);
#else
    readZ(wz, &a1, &b1);
#endif
// NOTE/BUG: a1 and b1 are _unitialized_ -- assume that above readZ needs to
// be changed
    double complex z1 = a1 + b1 * I;

    writeZ(wc, z1);
    printf("%.2f, %.2f\n", creal(z1), cimag(z1));

// NOTE/BUG: _pointers_ r and s are _unitialized_
#if 0
    readZ(wz, r, s);
#else
    readZ(wz, &a2, &b2);
#endif

// NOTE/BUG: a2 and b2 are _unitialized_ -- assume that above readZ needs to
    double complex z2 = a2 + b2 * I;
    printf("%.2f, %.2f\n", creal(z2), cimag(z2));

// NOTE/BUG: arguments are backwards if you want csum* to be the _sum_
#if 0
    sumZ(p, q, r, s, t, u);
#endif
#if 0
    sumZ(a1, b1, a2, b2, &csum1, &csum2);
#endif
#if 1
    sumZ(z1, z2, &csum1);
#endif

    printf("%.2f, %.2f\n", creal(csum1), cimag(csum1));

    fclose(wz);
    fclose(wc);

    return 0;
}

这是个清理过的版本,它改变了... readZ 还一个 double complex 直接,这[可能]更接近原意。

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <complex.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

void
readZ(FILE *wp, double complex *rtn)
{
    char c;
    double a;
    double b;

    assert(fscanf(wp, "%c%lg%c%lg%c%c", &c, &a, &c, &b, &c, &c));

    *rtn = a + b * I;
}

int
writeZ(FILE *wp, double complex z)
{

    fprintf(wp, "%.2f, %.2f\n", creal(z), cimag(z));

    return 1;
}

void
sumZ(double complex a1, double complex b1, double complex *c1)
{
    *c1 = a1 + b1;
}

int
main(int argc, char *argv[])
{
    FILE *wz;
    FILE *wc;
    double complex csum1;

    if (argc != 3) {
        printf("Wrong arguments number\n");
        printf("I should run this way:\n");
        printf("%s source result\n", argv[0]);
        exit(1);
    }

    if ((wz = fopen(argv[1], "r")) == NULL) {
        printf("Open error %s\n", argv[1]);
        exit(1);
    }
    if ((wc = fopen(argv[2], "w")) == NULL) {
        printf("Open error %s\n", argv[2]);
        exit(2);
    }

    double complex z1;
    readZ(wz, &z1);
    writeZ(wc, z1);
    printf("%.2f, %.2f\n", creal(z1), cimag(z1));

    double complex z2;
    readZ(wz, &z2);
    printf("%.2f, %.2f\n", creal(z2), cimag(z2));

    sumZ(z1, z2, &csum1);
    printf("%.2f, %.2f\n", creal(csum1), cimag(csum1));

    fclose(wz);
    fclose(wc);

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.