Question during complex number structure implementation! c2039 Error

Asked 2 years ago, Updated 2 years ago, 149 views

#include<stdio.h>
#include<math.h>

struct complex
{
    double real;
    double imag;
};

void add(struct complex* p, struct complex* q, struct complex* r);
void multiply(struct complex* p, struct complex* q, struct complex* r);
int main()
{
    struct complex p;
    struct complex q;
    struct complex r;
    scanf("%lf %lf", p.real, p.imag);
    scanf("%lf %lf", q.real, q.imag);
    add(&p, &q, &r);
    multiply(&p, &q, &r);
}

void add(struct complex* p, struct complex* q, struct complex* r)
{
    r->real = p->real + q->real;
    r->imag = p->imag + q->imag;
    printf("%lf %lf", r->real, r->imag);
}

void multiply(struct complex* p, struct complex* q, struct complex* r)
{
    r->real = ((p->real) * (q->real)) - ((p->imag) * (q->imag));
    r->imag = ((p->real) * (q->imag)) - ((p->imag) * (q->real));
    printf("%lf %lf", r->real, r->imag);
}

It's a complex number and multiplication problem, but it's a c2039 error and it doesn't work because it's not a member. Why is that?

Thank you!

struct c

2022-09-20 16:23

1 Answers

scanf("%lf %lf", p.real, p.imag);
scanf("%lf %lf", q.real, q.imag);

Change the code above as below.

scanf("%lf %lf", &p.real, &p.imag);
scanf("%lf %lf", &q.real, &q.imag);

You must specify the address of the variable when you store the value in a variable with scanf. The address of the variable if you prepend & to the name of the variable.


2022-09-20 16:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.