1

유형 구조체 struct variables, 과 함께 의사 결정문을 사용하는 데 문제가 있습니다. 변수를 어디에도 0으로 선언 할 수 없습니다. 여기 유형 def 구조체 변수가있는 조건문을 사용하는 데 문제가 있습니다.

는 구조체

typedef struct{ 
    int wallet; 
    int account; 
}MONEY; 

그것은 MONEY money;으로 메인에 선언이다 내가이

그런 짓을하려고하면

(하지만 *의 m와 문제가 함수 메신저에 대한)

*m.wallet = *m.wallet - depositAmmount; 

하거나

*m.wallet = 0; 

표현식에 클래스 유형이 있어야한다는 구문 오류가 발생합니다. 나는 무엇을합니까? 함수 내부에는 내가 아무 소용이 아래

에 INT *m.wallet = 0; 심지어 DATE *m.wallet;를 선언 시도하면 -> 연산자를 사용하는 구조체에 대한 포인터의 멤버에 액세스하려면 전체 기능

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#include <math.h> 
#define clear system("clear") 
#define pause system("pause") 
void banking(MONEY *m){ 
    int userChoice = 0; 
    int withdrawAmmount = 0; 
    int depositAmmount = 0; 
    do{ 
     userChoice = 0; 
     withdrawAmmount = 0; 
     depositAmmount = 0; 
     displayBankMenu(*m); 
     scanf("%i", &userChoice); 
     clear; 

     switch (userChoice) { 
      case 1: 
        printf("How much would you like to Deposit?: $"); 
        scanf("%i", &depositAmmount); 
        if(depositAmmount <= *m.wallet){ 
         *m.wallet = *m.wallet - depositAmmount; 
         *m.account = *m.account + depositAmmount; 
        }else{ 
         printf("You do not have sufficient funds for this transaction."); 
         pause; 
        } 

        break; 
      case 2:     
        printf("How much would you like to withdraw?: $"); 
        scanf("%i", &withdrawAmmount); 
        if(withdrawAmmount <= *m.account){ 
         *m.account = *m.account - withdrawAmmount; 
         *m.wallet = *m.wallet + withdrawAmmount; 
        } else{ 
         printf("You do not have sufficient funds for this transaction."); 
         pause; 
        } 

        userChoice = 0; 

        break; 
     } 
    }while(userChoice != 3); 
} //end banking 

답변

3

입니다.

m->wallet = m->wallet - depositAmmount; 
m->wallet = 0; 

. 오퍼레이터는 * 역 참조보다 높은 우선 순위를 갖고 있으므로 구조의 부재를 얻을 그 표현 괄호를 사용할 필요가있다.

(*m).wallet = (*m).wallet - depositAmmount; 
(*m).wallet = 0; 
+0

정말 고마워요! 나는 지금 이해한다. 그리고 나는 장래의 참조를 위해 두 번째 부분을 기억할 것이다! :) – user3027779

+0

대단히 환영합니다. – jxh

2

구조 부재 연산자 .는 참조 연산자 *보다 높은 우선 순위를 갖는다. 그래서 다음과 같은 성명

*m.wallet = 0; 

는 그것은 포인터의 가정 구조체 m의 회원 wallet를 얻을 후 역 참조 의미

*(m.wallet) = 0; 

과 동일합니다. 그러나 m은 구조 자체가 아니라 구조에 대한 포인터입니다. 따라서 먼저 포인터를 역 참조하여 구조체를 가져온 다음 . 연산자를 사용하여 wallet 멤버를 가져와야합니다.

(*m).wallet = 0; 

또한

m->wallet = 0; 

로보다 편리 화살표 연산자 ->을 사용할 수 있습니다 그들은 동일합니다.