Reputation: 27
#include <stdio.h>
#include <string.h>
struct student
{
char name[50];
int St_No;
float mark[10];
};
int main()
{
int entrynumber=0;
int r=0;
while(r != 1)
{
printf("choose one of the following options\n");
printf("1-Introduce new student\n");
printf("2-Introduce new mark\n");
printf("3-Print report of all students\n");
printf("4-Exit\n");
char givenname[50];
int option;
scanf("%d",&option);
int n,i,e=0,j=0;
struct student *St[50], Stu[50];
St[50] = &Stu[50];
switch(option)
{
case 1:
entrynumber;
printf("Enter name of student\n");
scanf("%s",(*St[entrynumber]).name);
printf("\nEnter student Number\n");
scanf("%d",&(*St[entrynumber]).St_No);
entrynumber++;
int noofstudents = entrynumber;
break;
|
I cut the rest out
OK so i get seg fault in this line here
scanf("%s",(*St[entrynumber]).name);
the scanf
works when i put it in, it just gives a seg fault
can anyone help me pls?
Upvotes: 0
Views: 97
Reputation: 782717
St[50] = &Stu[50];
is not the way to set all the pointers in St
to point to the corresponding elements of Stu
. You need a loop:
for (int i = 0; i < 50; i++) {
St[i] = &Stu[i];
}
BTW, (*St[entrynumber]).name)
is usually written as St[entrynumber]->name
.
There's really no need for the array of pointers at all. You can just use Stu[entrynumber].name
and &Stu[entrynumber].St_No
in your scanf()
calls.
And don't forget that %s
in scanf()
just reads a single word. If you try to enter a first and last name, it won't work. You probably should read a whole line with fgets()
(and don't forget to remove the newline at the end).
Upvotes: 4