2014-01-19 9 views
-2

구조체를 함수의 매개 변수로 사용하려면 어떻게해야합니까?구조체를 함수의 매개 변수로 사용

show(f,n,student); //n is the number of students 

그리고이 쇼 기능입니다 :

, 내가 파일에 데이터를 기록하는 기능이라고 읽고 구조에 정보를 저장 한 후

struct 
{ 
    char name[30]; 
    char section[20]; 
    float grade; 
}student[30]; 

: 나는이 시도

void show(FILE *f,int n, struct elev); 
{ 
... 
} 

감사합니다. 그들은 여러 학생을 가지고 있기 때문에

struct student_st { 
    char name[30]; 
    char section[20]; 
    float grade; 
}; 

, 당신은 아마 (배열)에 대한 포인터를 전달하려는 :

+0

http://stackoverflow.com/questions/13823886/c-passing-struct-as-argument –

+0

좋은 C 프로그래밍 책을 읽으려면 시간이 많이 걸릴 것입니다. –

답변

2

당신은 이름 당신의 구조 더 나은 것

void show(FILE *f,int n, struct student_st* s) { 
    assert (f != NULL); 
    assert (s != NULL); 
    for (int i=0; i<n; i++) { 
    fprintf(f, "name: %s; section: %s; grade: %f\n", 
      s->name, s->section, s->grade); 
    }; 
    fflush(f); 
} 

다음과 같이 사용하십시오 :

#define NBSTUDENTS 30 
struct student_st studarr[NBSTUDENTS]; 
memset (studarr, 0, sizeof(studarr)); 
read_students (studarr, NBSTUDENTS); 
show (stdout, NBSTUDENTS, studarr); 

자세히 알아보기 arrays are decaying into pointers.

관련 문제