Reputation: 258
I'm trying to create a program to manage a university, to do that I think of creating a stuct
student and put another struct
branch inside of it. this is what I've done, which is not working well
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] $
I'm trying for two days to fix this problem, can anyone help me understand where is the problem and how to fix it
Upvotes: 2
Views: 71
Reputation: 134336
The very first problem is in
scanf("%s", student->Stud_name);
in your case, Stud_name
is a char
which is not sufficient to hold a string, (let alone you missed to pass the address part). You need an array there.
Attempt to read a string into a char
with %s
format specifier invokes undefined behavior.
Try to change the definition of Stud_name
from
char Stud_name;
to
#define NAMESIZ 32
. . . .
char Stud_name[NAMESIZ];
Then, coming to the point I left aside earlier, you need to pass the address of the variable to the scanf()
as the argument to format specifier. Something like
scanf("%f",&(student->deg[k]));
and so on.
Upvotes: 2