【发布时间】:2016-02-09 19:55:26
【问题描述】:
代码
我正在尝试创建一个程序来管理一所大学,为此我想创建一个stuct student 并在里面放另一个struct branch其中。这是我做的,效果不好
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()
正在编译..
[ar.lnx@new-host 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.
[ar.lnx@new-host src-new] $
正在执行
[ar.lnx@new-host src-new] $ ./x
10
Enter the Full name
anas
Segmentation fault (core dumped)
[ar.lnx@new-host src-new] $
我尝试了两天来解决这个问题,任何人都可以帮助我了解问题出在哪里以及如何解决它
【问题讨论】:
-
student.c 不完整。特别是,我没有看到产生警告的代码行。
-
标准保留以
_和大写字母开头的标识符。 不要在应用程序代码中使用它们。