2014-11-23 4 views
-4

여기 내 질문은 항상 함수를 사용하는 것 같습니다. 그것은 여전히 ​​나를 혼란스럽게합니다! 이 교과서 연습에서는 값으로 구조체를 전달한 다음 조정하여 참조로 전달하도록합니다. 처음에는 모든 것을 메인에서 처리하도록 코드를 디자인했습니다. 이제 저는 가치있게 지나가고 있습니다. 그래서 새 함수를 추가하고 구조체를 올바르게 전달했다고 생각했지만 라인에서 오류가 발생했습니다 void function1 (구조체 인벤토리 inv) {이는 매개 변수 1 (inv)에 불완전한 형식이 있음을 나타냅니다. 도와주세요!C - 구조체 + 멤버 값으로 전달

#include <stdio.h> 
#include <conio.h> 
#include <stdlib.h> 

void function1(struct Inventory inv); 

struct Inventory{ 
    char name[20]; 
    int number; 
    float price; 
    float total; 
} 

void main(){ 

    items; 

    void function1(items); 

    float total = items.number*items.price; 
    printf("Item\tNumber\tPrice\tTotal\tAddress of number\n"); 
    printf("%s\t%d\t%.2f\t%.2f\t%X\n\n",items.name,items.number,items.price,total,&items.number); 

    getch(); 
} 

void function1(struct Inventory inv) { 

    printf("Enter the name of the item: "); 
    scanf("%s", inv.name); 

    printf("Enter the number of items: "); 
    scanf("%d", &inv.number); 

    printf("Enter the price of each item: "); 
    scanf("%f", &inv.price); 
} 
+2

이것은이 사이트에서 이미 세 번째 질문입니다. 더 이상 게시하기 전에 * 들여 쓰기를 배우십시오 *. –

+0

비 기본 유형을 값으로 전달하는 것은 비효율적이며 무의미합니다. 포인터를 사용하는 법을 배우십시오. – Havenard

+0

나의 추측은 값으로 지나가는 것과 참고로 넘어가는 것의 차이를 가르쳐 준다는 것입니다. 비록 포인터가 가장 좋은 방법입니다. – steeele

답변

1

함수 프로토 타입에 사용하기 전에 구조체를 정의해야합니다.

struct Inventory{ 
char name[20]; 
int number; 
float price; 
float total; 
}items; 

void function1(struct Inventory inv);