0

Declared structures:

typedef struct{ char fname[25]; char mname[3]; char lname[25]; }Name; typedef struct{ char month[25]; int day,year; }Date; typedef struct{ Name gname; char addr[50]; char cnum[11]; }Guardian; typedef struct{ Name sname; Date sbday; Guardian sguard; char gender[6]; char addr[50]; char cnum[11]; char course[10]; int year; }Student; 

Declared functions:

void input(Name name,Date date, Guardian guard, Student stud); void display(Name name,Date date, Guardian guard, Student stud); 

When input function is called, it lets me add strings being asked. Then, display function is called. It will not display the entered information.

Calling functions:

input(name, date, guard, stud); display(name, date, guard, stud); 

Display funtion:

void display(Name name, Date date, Guardian guard, Student stud) { printf(" -=Student Information=- \n"); printf("Name: %s %s. %s\n",stud.sname.fname,stud.sname.mname,stud.sname.lname); printf("Birtday: %s %d, %d\n",stud.sbday.month,stud.sbday.day,stud.sbday.year); printf("Gender: %s\n",stud.gender); printf("Contact Number: \n%s",stud.cnum); printf("Course & Year: %s-%d \n",stud.course,stud.year); printf(" -=Student Guardian Information=- \n"); printf("Name: %s %s. %s\n",guard.gname.fname,guard.gname.mname,guard.gname.lname); printf("Address: %s\n",guard.addr); printf("Contact Number: %s\n",guard.cnum); } 

Input function:

void input(Name name,Date date, Guardian guard, Student stud) { printf("Student Information \n"); printf("First Name: "); gets(stud.sname.fname); //... } 

This will just display one set of information. It won't add new records per say. Just a simple exercise.

5
  • Instead of fixed-length character fields, you're going to want to use char* and dynamically allocate. Some of these buffers are ridiculously short. Commented Mar 14, 2018 at 16:58
  • 1
    You're also passing in parameters by value instead of as a pointer, something that's extremely messy and wasteful. Commented Mar 14, 2018 at 16:59
  • 2
    Problem is that C uses pass-by-value, so you just modify copies of original structs, not struct themselves. Pass addresses. Commented Mar 14, 2018 at 16:59
  • 1
    Never ever use gets. It's a dangerous function which have been removed from the C specification. Use alternatives like fgets instead. Commented Mar 14, 2018 at 16:59
  • btw..to add.. This will just display 1 set of information.. It won't add new records per say.. Just a simple exercise Commented Mar 14, 2018 at 17:03

2 Answers 2

3

C is pass by value. So when you passed a variable as you have shown - a copy of it is given to the called function so that it can work with it. As a result the object from which the copy is made remains unchanged. The solution is to pass the address of the object and then dereferencing the copied pointer variable which contains the address of the object - thus changing the intended object.

So in your case,

void input(Name *name,Date *date, Guardian *guard, Student *stud){ printf("%s",name->fname); // short hand for (*name).fname ... } 

Call it like

input(&name, .. ); 

Even if you want only read those objects(you don't want to change anything) then also I would support this way of doing things - why burden things by copying those large structure instances? Just pass the address of them and work with it.

Also don't use deprecated gets - use fgets instead and when you use it (fgets) don't forget to check the return value.

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

3 Comments

since the guardian struct and student struct has a common structure which is Name.. I don't think Name -> fname would work since. Ill have to do guard.gname.fname etc.
@EricMichaelFadriga.: Hey I am giving you a example...and I wrote name->fname here it is a variable name. You get what I am saying?
ohh wow it worked!!! :D Thanks man!! :D I forgot to place a * in the function declaration..
0

Like how to modify the content of a variable througth a function, you need to give the address of the structure, because "deep down", a variable of type "int" and a variable of type "struct something", well, it's a variable.

And if you want to modify the value of your variable, you need to send his address (pointer).

Also, note that by passing the value of the struct, the value are copied. For small structure, it doesn't really matter, but for "larger" one, it will cost really huge on your program's speed.

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.