Ad Code

6/recent/ticker-posts

Passing structure member as Argument in Data Structure

                                                              

Pass a structure member as Argument 

In previous posts we have understood the various types of pointers and definition of data structure. Check here

Now lets move further, structure is a user defined data type that can be used to group the elements different types into a single type.

The topics we are going to cover in our subsequent blogs are,

1.Passing structure member as argument 

2.Passing structure variable as argument  

3.Passing pointers to a structure as arguments 

4.Returning the structure variable from function 

5.Returning a pointer to structure from a function 

6.Passing array of structures as argument  

7.Self referential structure

Passing structure member as Argument 

We can pass structure members to the function just like we pass the variables.
lets understand through the example,

struct student {
    char name[50];
    int age,roll;
};
 void print (char name[], int age, int roll){
     printf("%s,%d,%d", name,age,roll);
 }
 
 int main(){
     struct student s1={"Niraj",20,30};
     print(s1.name,s1.age,s1.roll);
     
 }


 Here we have created a one programme which gives the details of student.
First we have created a structure name student. In which we have declared various variables or different data type. As we know structure is nothing but group elements having different data types.
Now moving further we have declared print function which basically prints the name of student, age and roll number of student.
Then in main function were giving details and simply printing it

Call by reference 

We can pass he address of the structure members instead of passing the copies of members.
check this example,

struct chareset{
    char s;
    int i;
    
};
 void key(char* s, int* i){
     scanf("%c,%d",s,i);
 }
 
 int main(){
     struct chareset cs;
     key(&cs.s, &cs.i);
     printf("%c,%d",cs.s, cs.i);
     return 0;
 }


Further topics will be covered in next post.

Post a Comment

0 Comments

Ad Code