如何从函数中的指针添加两个值?

How to add two values from a pointer in function?

我的老师说我必须使用复数类型将两个复数相加。我必须在函数中添加这些数字。怎么做? 我创建了类似的东西,但是当我使用 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 我得到一个分段错误。他说我可以用引用和指针。我不知道我现在应该做什么。

有一些错误。

有很多 double * 指针变量 [被初始化]。他们不是真的需要。 main 可以向下传递(例如)&a1&b1。仅仅因为您被告知要使用指针并不 [必然] 意味着您必须使用指针变量。

此外,只有 return 值才需要指针作为函数的参数。其他人可以只使用 "pass by value" 的变量。这稍微简化了事情。

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

您的代码仍在尝试使用单独的 double 变量(例如)a1b1 来获取 z1 而不是 [mostly] 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 更改为 return a 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;
}