Krestek Krestek - 1 year ago 59
C Question

Getting a runtime error when passing a pointer of a struct to a function. (in C Language)

I have this in Header.h:

typedef struct student_struct{
int id;
char name[30];
struct student_struct* next;
} student;

typedef struct stlst{
student* head;

in Header.C:

void add_element(studentlist* list,char name[30],int id){
printf("adding an element");

in Main.c:

int main(){
studentlist list;
add_element(&list, "name",4);

Whenever I run the program to test it I get an error saying "Segmentation fault.". It doesn't print "adding an element". The program stops after passing the pointer to studentlist to the "add_element" function.

What is the cause and can you provide an example of how to fix such error?

Edit: Tried to make a minimal example, but it worked with no errors.. Here are my files, download them, and test them yourself, I don't know why but it only gives errors when executed after compiling from these files:

Answer Source

If one looks at your actual code from the MediaFire link, which - in line with the massive mess that is the rest of this thread - is totally different from what you've posted in the body of your question... we can see that:

  • add_element() actually does attempt to read from studentlist list, which is declared but left uninitialised in main(), as joop initially observed.
  • your actual code doesn't call add_element()... but it does call remove_element(), which also depends on list->head being initialised to a defined value, among other things.

Learn, and learn well:

People often misrepresent uninitialised variables by saying 'they will have a random value'. That would be bad enough and already disqualify any program using them from ever being useful. But it's worse than that. They don't get a random value. The read invokes UB. Any read of an uninitialised variable renders the rest of your program ill-formed, which means the compiler can transform it into something totally different, or a no-op, or anything.

So maybe the invalid list->head leads your program into memory that's not its own, hence a segfault, or maybe it simply causes entire swathes of your program to be obliterated or optimised (because UB 'cannot happen', so code involving UB and its results can be deleted) and thereby cause some totally different preconditions to fail. Who knows? or cares. You just need to fix it.

Please, help yourself to solve problems, at the stage where you are uniquely placed to solve them, before expecting other people to guess or go through the whole process again:

  • Always compile with all possible warnings enabled, as they would have yelled at you about the use of uninitialised variables
  • Use a debugger. I know you said you can't, but if "this is not my pc and i can't install anything else or use any different tools", then whose PC is it? If they're asking you to write code for them, surely they want you to be able to write good code by properly diagnosing problems?

Had you simply posted the actual code in the beginning, this thread would have been over hours ago, saving everyone involved from various degrees of wasted time, energy, and headaches.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download