0

So the question I'm working on has me creating a structure with 5 elements: id number, name, department, course, and year. I have to write a function that prints the names of people with certain years and a function that prints all data based on id number input.

When I type in 2020 as an input I get a segmentation fault. What could be causing this?

#include <stdio.h> struct stud { int id; char name[50]; char dep[50]; char course[50]; int year; }; void printbyyear(struct stud *student,size_t sz, int a); void printspecific(struct stud *b,size_t sz, int a); int main() { int yr,num,b; struct stud students[]={ {1,"Jon","math","calc",2019}, {2,"Tim","Language arts","calc",2020}, }; printf("Input year to search for:"); scanf("%d",&yr); printbyyear(students, sizeof(students),yr); printf("Input ID # to search for:"); scanf("%d",&num); printspecific(students, sizeof(students),num); return 0; } void printbyyear(struct stud *b,size_t sz, int a) { int i; for(i=0;i<sz;i++) { if (b[i].year==a) { printf("%d %c %c %c %d",b[i].id,b[i].name,b[i].dep,b[i].course,b[i].year); } } } void printspecific(struct stud *b,size_t sz, int a) { printf("%d %c %c %c %d",b->id,b->name,b->dep,b->course,b->year); return 0; }``` 
5
  • 1
    The variables of type struct stud that you've created are b1 and b2. One of those is what you should be passing as the first argument to printbyyear()---not struct stud, which is not a variable but the name of a type. Commented Dec 10, 2020 at 19:02
  • typo? printbyyear(struct stud,yr); Commented Dec 10, 2020 at 19:18
  • Also, it is more common to pass large structs by address. Commented Dec 10, 2020 at 19:32
  • scanf("%d...", & b[i].id, ... . That is, use &b[i].id instead of b[i].id. Similarly in other places where appropriate. Commented Dec 10, 2020 at 19:50
  • .... but if you are reference b[i] at all, then I would expect to see the function signature as void printbyyear( struct stud *b....) and the structure's address should be passed. Commented Dec 10, 2020 at 19:52

3 Answers 3

1

There are many mistakes in your programs.

  1. printbyyear(struct stud,yr);

that is not how you call a function with struct variable, change it as printbyyear(b1,yr); or printbyyear(b2,yr);

  1. you are not using array of structs , so you don't need to use loops at all for your code.
if (b.year == a) { printf("%d %s %s %s %d",b.id,b.name,b.dep,b.course,b.year); } 

finally if you want to store multiple records, then use array of struct's like below.

struct stud students[]= { {2,"Tim","Language arts","calc",2020}, {1,"Jon","math","calc",2019} }; 

and call it as below ( sizeof(students)/sizeof(students[0]) ) gives the number of elements you have in your structure)

printbyyear(students, sizeof(students)/sizeof(students[0]), yr); 

now you can use loops for printing because you passed an array

void printbyyear(struct stud *student, size_t sz, int y) { int i; for(i=0;i<sz;i++) { if (student[i].year == y) { printf("%d %s %s %s %d",student[i].id,student[i].name,student[i].dep,student[i].course,student[i].year); } } } 

and you declaration of function should be as below

void printbyyear(struct stud *b,size_t size, int a);

Sign up to request clarification or add additional context in comments.

4 Comments

So now the program compiles, which is an improvement, but now after I enter my desired year it just gives a segmentation fault. Is something not pointing to the right spot?
edit your original post with changes you have done
I edited the code and reviewed what I've done and i don't know where the code is getting stopped
you have not copied the code properly, just look at printbyyear(students, sizeof(students),yr); and how i called 2) format specifiers are not updated in printf inside your functions use %s instead of %c 3) for printing by id you have to compare the same way as you are comparing in if (b[i].year==a), but instead of year, it should be id
0

I guess that here

if (b[i]stud.year==a) 

You are trying to access b1, b2 and any other possible student. To do that you need an array of type struct stud

struct stud b[5]; 

Now your if condition makes sense. The function call should be:

printbyyear(b,yr); 

And the declaration

void printbyyear(struct stud *b, int a) 

Also since you initialize only two students, i.e. only two elements of your array, the for loop should be for(i=1;i<=2;i++) with a 2 not a 5.

Accessing uninitialised elements or accessing an array out of bounds will result in an undefined behaviour.

2 Comments

While this answers explains how to access b[i] it does not describes how to prevent reading or writing beyond the array limits. The function should also receive as argument the array size (the number of struct stud items in it, 5 in this case) and it should make sure i is inside the array limits before reaching b[i].
Yes, you are right. I am going to add something about it, even if that thing of accessing an array out of bounds is a matter of arrays, which is not what the OP asked for. But yes, why not to just help him in any way?
0

Correct is an ambiguous term, so I will just say this method is one that I prefer:

Passing the address of an object of significant size is advantageous in that the address of that object is going to be smaller than the object itself.

So given for example your struct (typedefed for readability) I would make the following modifications (i.e. to pass by reference to the address of the object.)

Read in-line comments and note corrections in code:

 typedef struct { int id; char name[50]; char dep[50]; char course[50]; int year; }stud_s; //modify to accept pointer, and add size of array argument void printbyyear(stud_s *s, int yr, size_t size); int main() { int yr,num; stud_s b1[] = { {1,"Jon","math","calc",2019}, {2,"Tim","Language arts","calc",2020}, {3,"Jen","the arts","calc",2021}, {4,"Bob","painting arts","calc",2022}, {5,"jed","numeric arts","calc",2023} }; size_t size = sizeof(b1)/sizeof(b1[0]); printf("Input year to search for:"); scanf("%d",&yr); printbyyear(b1,yr, size);// b1 is a pointer to array of struct here. printf("Input ID # to search for:"); scanf("%d",&num); return 0; } void printbyyear(stud_s *b, int a, size_t size) { int i=0; //for(i=1;i<=5;i++)//results in out of bounds access to array for(i=0;i<size;i++) //change magic number for actual size of array { if (b[i].year==a) { //fix format specifiers printf("%d %s %s %s %d\n",b[i].id,b[i].name,b[i].dep,b[i].course,b[i].year); //printf("%d %c %c %c %d",b[i].id,b[i].name,b[i].dep,b[i].course,b[i].year); i++; } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.