복소수 구조체 구현 중 질문입니다! c2039오류

조회수 1084회
#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);
}

복소수 합, 곱 구하는 문제인데 c2039오류 라면서 맴버가 아니라고 실행이 안되는데 왜 그런건가요?

감사합니다!

1 답변

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

    위의 코드를 아래와 같이 바꾸세요.

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

    scanf로 값을 변수에 저장할때에는 변수의 주소를 지정해 줘야 합니다. 변수이름 앞에 &를 붙이면 변수의 주소입니다.

    • (•́ ✖ •̀)
      알 수 없는 사용자
    • 답변 감사합니다 . 그런데 아직도 (오류 C2039 'imag': '_complex'의 멤버가 아닙니다.)가 뜹니다 ㅜㅜ 알 수 없는 사용자 2021.5.17 15:47
    • r도 scanf로 받아야할텐데 질문의 코드엔 r에 관한 scanf 함수가 빠져 있네요. 위 답변의 scanf 함수들 밑에 scanf("%lf %lf", &r.real, &r.imag);를 추가하세요. 알 수 없는 사용자 2021.5.17 19:19
    • 아...Complex로 해야하네요 대박... 알 수 없는 사용자 2021.5.17 23:18

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)