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.
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.
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.
Related
Hi friends I am practicing structures. I have these two functions one returns the structure and I copy that to the local struct in main. My second function changes those local struct members by entering different entities.
Now I have printed result after calling each function, to my surprise I notice that printed result after both the function are same. I am unable to understand what is happening here…can you guys please explain me…thanks!
#include <stdio.h>
#include <stdlib.h>
struct student{
char name[30];
float marks;
};
struct student read_student();
void read_student_p(struct student student3);
void print_student(struct student student2);
int main()
{
struct student student1;
student1 = read_student();
print_student(student1);
read_student_p(student1);
print_student(student1);
system("pause");
return 0;
}
//This is my first function
struct student read_student()
{
struct student student2;
printf("enter student name for first function: \n");
scanf("%s",&student2.name);
printf("enter student marks for first function:\n");
scanf("%f",&student2.marks);
return student2;
}
//function to print
void print_student(struct student my_student)
{
printf("Student name in first function is : %s\n",my_student.name);
printf("Student marks in first function are: %f\n",my_student.marks);
};
//My second function
void read_student_p(struct student student3)
{
printf("enter student name for second function: \n");
scanf("%s",&student3.name);
printf("enter student marks for second function: \n");
scanf("%f",&student3.marks);
}
Do you mean to write
void read_student_p(struct student* student3)
^
{
read_student_p(&student1);
^
You need to pass a pointer to read_student_p if you want to modify the struct that you are passing. Currently it is passed by value, and the modifications are lost.
Considering the _p suffix, I expect that this was intended..
When you do this:
read_student_p(student1);
And the method looks like this:
void read_student_p(struct student student3)
{
printf("enter student name for second function: \n");
scanf("%s",&student3.name);
printf("enter student marks for second function: \n");
scanf("%f",&student3.marks);
}
Structs in C are passed by value, not by reference.
So what read_student_p does is take a copy of the struct you pass in (student1), edit the copy, and then do nothing.
One solution would be to return the changed version of the struct. Another version would be to pass a pointer to a struct, and edit the struct via pointer (so that you're editing the same copy of the struct directly).
In the second function read_student_p you were calling by value, which is to say, you defined a new struct variable tmp in the function, and copied the value of student1 to this tmp value. All the modifications that you've done were on tmp value, which wouldn't affect student1.
struct Name { // structure
char firstName[31];
};
int main(void) {
struct Name name; // structure type variable
void getName(struct Name *name);
printf("First name: %s\n", name->firstName); //printing error occurring
}
void getName(struct Name *name) { //function definition
printf("Please enter the contact’s first name: ");
scanf("%s", name->firstName); //taking the input from user
}
In my code I am trying to create a contact management system. I have structure a function and a main function. What I am doing is I have a structure, I am passing that structure to a function. The function gets an input for the structure member and stores it. But there's something wrong with my syntax while calling the getName() function and while printing the structure member input that I took from the user.
The error is in the way you are accessing member of the object. It should be accessed like:
name.firstName instead of with -> operator. This is because you have an object and not a reference (pointer) to an object.
So the correct prints call is:
printf(“First Name: %s\n”, name.firstName);
i need to input name to a variable (first name*) in a struct
with a malloc
i don't understand why to program is fail to run.
im inserting the name (for example David)
and its should gets the name and put it in temp array
and then to resize the pointer first_name*
and copy the string temp to first_name*
someone can help me understand why its doesn't work?
look for the function "ReadPerson".
typedef struct{
int day, month, year;
} Date;
typedef struct{
char *first_name, *last_name;
int id;
Date birthday;
} Person;
void ReadDate(Date *a)
{
printf("insert day, month, year\n");
scanf("%d%d%d", &a->day, &a->month,&a->year);
}
void ReadPerson(Person *b)
{
char temp_first_name[21];
char temp_last_name[21];
printf("insert first name:\n");
gets(temp_first_name);
b->first_name = (char*)malloc(strlen(temp_first_name)+1);
strcpy(b->first_name,temp_first_name);
//need to check malloc (later)
printf("insert last name:\n");
gets(temp_last_name);
b->last_name = (char*)malloc(strlen(temp_last_name)+1);
strcpy(b->last_name, temp_last_name);
//need to check malloc (later)
printf("insert id\n");
scanf("%d",&b->id);
printf("insert person's birthday:\n");
ReadDate(b);
}
Thanks.
i don't understand why to program is fail to run
Well, it's because you're trying to substitute incompatible types and decent compiler should have told you about that.
Let's look at the end of function void ReadPerson(Person *b):
{
...
ReadDate(b); // error here
}
As you can see b is of type Person * and you pass it to the function void ReadDate(Date *a) which expects a Date * type.
So it is probably a simple typo, just change to this: ReadDate(&b->birthday);.
#include<stdio.h>
#include<string.h>
struct employee
{
int id;
char name[20];
float salary;
char addr[];
}*emp;
void get_emp(struct employee **record)
{
printf("\tplease enter details of employee:\n");
printf("id= ");
scanf("%d",(*record)->id);//code to get input value
printf("Name= ");
scanf(" %s",(*record)->name);
printf("salary= ");
scanf("%f",(*record)->salary);
}
int main()
{
get_emp(&emp);
printf("id=%d\n",emp->id); // code to display the value
printf("Name=%s\n",emp->name);
printf("salary=%f\n",emp->salary);
return 0;
}
I have a structure example and I want to pass structure pointer to the function without using normal variable but only pointer. Is the parameter(double pointer) in the function get_emp(struct employee **record) a correct way to do if not what changes should be made? Also how to get input value from users in the function get_emp(struct employee **record) and how to display the value?
A double pointer is not needed. You can accomplish this with single pointer as well. You would however need to allocate memory for the structure before populating it.
struct employee *emp = malloc(sizeof(struct employee));
Also the size of char addr[] in your structure is undefined. That needs to be allocated separately similar to:
emp->addr = malloc(N*sizeof(*emp->addr));
All other fields in the structure which has a fixed size such as integer,float and a character array of 20 elements does not need allocation. The allocation for them would be done by malloc we did earlier. However since addr is a pointer, memory equivalent to size of pointer would be reserved for it. To store anything in the address the addr points to, we need to allocate.
Afterwards pass emp to your function. Remove the double pointer
Hi friends I am practicing structures. I have these two functions one returns the structure and I copy that to the local struct in main. My second function changes those local struct members by entering different entities.
Now I have printed result after calling each function, to my surprise I notice that printed result after both the function are same. I am unable to understand what is happening here…can you guys please explain me…thanks!
#include <stdio.h>
#include <stdlib.h>
struct student{
char name[30];
float marks;
};
struct student read_student();
void read_student_p(struct student student3);
void print_student(struct student student2);
int main()
{
struct student student1;
student1 = read_student();
print_student(student1);
read_student_p(student1);
print_student(student1);
system("pause");
return 0;
}
//This is my first function
struct student read_student()
{
struct student student2;
printf("enter student name for first function: \n");
scanf("%s",&student2.name);
printf("enter student marks for first function:\n");
scanf("%f",&student2.marks);
return student2;
}
//function to print
void print_student(struct student my_student)
{
printf("Student name in first function is : %s\n",my_student.name);
printf("Student marks in first function are: %f\n",my_student.marks);
};
//My second function
void read_student_p(struct student student3)
{
printf("enter student name for second function: \n");
scanf("%s",&student3.name);
printf("enter student marks for second function: \n");
scanf("%f",&student3.marks);
}
Do you mean to write
void read_student_p(struct student* student3)
^
{
read_student_p(&student1);
^
You need to pass a pointer to read_student_p if you want to modify the struct that you are passing. Currently it is passed by value, and the modifications are lost.
Considering the _p suffix, I expect that this was intended..
When you do this:
read_student_p(student1);
And the method looks like this:
void read_student_p(struct student student3)
{
printf("enter student name for second function: \n");
scanf("%s",&student3.name);
printf("enter student marks for second function: \n");
scanf("%f",&student3.marks);
}
Structs in C are passed by value, not by reference.
So what read_student_p does is take a copy of the struct you pass in (student1), edit the copy, and then do nothing.
One solution would be to return the changed version of the struct. Another version would be to pass a pointer to a struct, and edit the struct via pointer (so that you're editing the same copy of the struct directly).
In the second function read_student_p you were calling by value, which is to say, you defined a new struct variable tmp in the function, and copied the value of student1 to this tmp value. All the modifications that you've done were on tmp value, which wouldn't affect student1.