2016-02-09 67 views
2

代碼

我試圖創建一個程序來管理一所大學,要做到這一點,我認爲建立一個stuct學生,並把另一struct分支它裏面。這是我做了什麼,這都不盡如人意無法訪問的結構membre

base.h

#define DEG_SIZE 2 
#define BRANCH_SIZE 5 
struct Branch{ 
     char Departement; 
     char Year; 
    }b; 
    typedef struct Branch _BRANCH; 
_BRANCH branch[BRANCH_SIZE]; 
    struct Student_informations{ 
     char Stud_name; 
     float deg[DEG_SIZE]; 
     char* Stud_year; 
     char Payment_State; 
     _BRANCH Stud_branch; 
    }c; 
    typedef struct Student_informations _STUDENT; 
enum Departement 
    { MATH,PHY, 
     CHY,GIO, 
     BIO,INFO 
    }; 
enum Year 
    { first,second, 
     licence,master, 
     doc 
    }; 
enum Payment 
    { 
     TRUE, 
     FALSE 
    }; 

student.c

/*<--------------SETUP-------------->*/ 

/*=============-MATH-=============*/ 
      //SMIA 
    branch[0].Departement = MATH; 
    branch[0].Year = first; 

      //SMI/SMA 
    branch[1].Departement = MATH; 
    branch[1].Year = second; 

      //SMF/SMA 
    branch[2].Departement = MATH; 
    branch[2].Year = licence; 

      //MASTER-MATH 
    branch[3].Departement = MATH; 
    branch[3].Year = master; 

      //DOCTURAT-MATH 
    branch[4].Departement = MATH; 
    branch[4].Year = doc; 
/*=============-MATH-=============*/ 
/*<--------------SETUP-------------->*/ 
#include "base.h" 
void set_students(_STUDENT* student) 
{ 
    puts("Enter the Full name"); 
    scanf("%s", student->Stud_name); 
    puts("Enter his deg"); 
     for(int k=0; k < DEG_SIZE; ++k){ 
      printf("deg[%d]", k+1); 
       scanf("%f",student->deg[k]); 
      puts(""); 
     }// end for 
    puts("enter the year"); 
     scanf("%s", student->Stud_year); 
    Stud_branch -> branch[2]; 
puts("DONE!"); 
}// end set_students() 

AF.C

#include "base.h" 

/*beginning of main()*/ 
int main(int argc,char *argv[]) 
{ 
    _STUDENT Student; 
    set_students(&Student); 
    return EXIT_SUCCESS; 
}//end main() 

編譯..

[[email protected] src-new] $ make 
gcc -I. -c -o af.o af.c 
gcc -I. -c -o student.o student.c 
student.c: In function ‘set_students’: 
student.c:23:25: warning: assignment makes integer from pointer without a cast [-Wint-conversion] 
    student->Payment_State = hold; 
         ^
Building the application core.. 
gcc -o x af.o student.o -I. 
Finish. 
[[email protected] src-new] $ 

執行

[[email protected] src-new] $ ./x 
10 
Enter the Full name 
anas 
Segmentation fault (core dumped) 
[[email protected] src-new] $ 

我想了兩天來解決這個問題,誰能幫我瞭解問題出在哪裏以及如何解決問題

+0

student.c不完整。特別是,我沒有看到產生警告的代碼行。 – dbush

+1

以'_'和大寫字母開頭的標識符由標準保留。 **不要在應用程序代碼中使用它們。** – Olaf

回答

2

的第一個問題是,你的情況

scanf("%s", student->Stud_name); 

Stud_namechar這是不是足以容納一個,(別說你錯過了通過地址部分)。你需要一個數組。

嘗試讀取一個char%s格式說明調用undefined behavior

嘗試的Stud_name定義從

char Stud_name; 

改變

#define NAMESIZ 32 
. . . . 
char Stud_name[NAMESIZ]; 

然後,來到我擱置一邊更早的時候,你需要的變量的地址傳遞給scanf()作爲格式說明符的參數。類似於

scanf("%f",&(student->deg[k])); 

等等。