Features of Structures in C programming language

We have already seen how structures are so useful in storing data which belong to different data types under the same name. Apart from being used in maintaining records or database management, structures have a lot more use like changing the size of the cursor or clearing the contents of the screen or hiding or displaying the file or directory. How can structures do so many things? Well, structures have a lot more features that we will see in this tutorial.

Features of structures

The features are listed below:

1) Using the “=” operator

Structure elements are stored in the memory in contiguous memory locations which allow us the use of “=” (the equals to assignment operator) to copy the entire elements of one structure to another. This is not possible in case of arrays where we need to copy all the elements one by one in order to copy the entire elements. Other than equating the two structure variables the user can also copy the elements piece-meal. But that would be more complex. Here is an example of how we can simply equate two structures to copy the values from one to another.

#include <stdio.h>
#include <string.h>
int main() {
  struct student { // declaring a structure to store student details
    char name[10];
    int id;
    float marks;
  };

  struct student s1 = {"Ram", 101,
                       79.0}; // we store the details of the first student
  struct student s2, s3;

  /*the piece meal copying or copying each element separately*/
  strcpy(s2.name, s1.name);
  s2.id = s1.id;
  s2.marks = s1.marks;

  /*copying the elements directly using '=' operator*/
  s3 = s1;
  /*Displaying the copied details*/
  printf("The details copied in s2:\n");
  printf(" Name= %s\n ID= %d\n marks= %f\n\n", s2.name, s2.id, s2.marks);
  printf("The details copied in s3:\n");
  printf(" Name= %s\n ID= %d\n marks= %f\n\n", s3.name, s3.id, s3.marks);
  return 0;
}
 
Output:-
The details copied in s2:
 Name= Ram
 ID= 101
 marks= 79.000000

The details copied in s3:
 Name= Ram
 ID= 101
 marks= 79.000000

2) Nested Structures

C programming allows us to create nested structures,i.e., one structure within another structure. This property enables us to create complex data types for storing various elements. The nesting process can continue as long as the user wants.

#include <stdio.h>
int main() {
  struct book { // declaring 1st structure
    char name[20];
    char author[20];
    int pages;
  };
  struct price {   // declaring 2nd structure
    struct book b; // nested the 1st structure inside the second one
    float price1;
  };

  /*initializing values*/
  struct price b1 = {"TinyTales", "G.Shaw", 110, 299.00};
  struct price b2 = {"FairyTales", "P.Saha", 270, 449.00};

  /*displaying the book details*/
  printf("The 1st book details are as follows:\n");
  printf(" Name= %s\n Author= %s\n pages= %d\n price= %f\n\n", b1.b.name,
         b1.b.author, b1.b.pages, b1.price1);
  printf("The 2nd book details are as follows:\n");
  printf(" Name= %s\n Author= %s\n pages= %d\n price= %f\n\n", b2.b.name,
         b2.b.author, b2.b.pages, b2.price1);
  return 0;
}

Output:-
The 1st book details are as follows:
Name= TinyTales
Author= G.Shaw
pages= 110
price= 299.000000

The 2nd book details are as follows:
Name= FairyTales
Author= P.Saha
pages= 270
price= 449.000000

Also, to access the elements of the nested structure we used the dot operator(.) twice so that the compiler knows that the element belongs to the nested structure and can access it.


3) Passing Structure Variables to a Function

This feature of structure allows us to pass any of the structure variables to the function like any other normal variable. This can be done in two ways: Passing individual structure elements to the function or passing the entire structure to the function. Let us have a look at both of them:

a) Passing individual elements

For passing individual elements we are using both ‘pass by reference’ and ‘pass by value’ method. Now, we already know that when we use the ‘by reference’ method, a copy of the original structure is not made and the changes made in the structure when passed to another function gets reflected in the original structure. The address of the original structure is passed. Whereas, in ‘by value’ method a copy of the structure is made and the changes made in the structure when passed to a function is not reflected in the original structure.  The following program stores the student details in a variable and displays them using another function:

#include <stdio.h>
void display(char *, int,
             float); // declaring function to display the student details
int main() {
  struct student { // declaring the structure
    char name[10];
    int id;
    float marks;
  };
  struct student s1 = {"Rohan", 102, 55.0};
  struct student s2 = {"Mohan", 103, 99.0};
  /*sending individual values to the function display()*/
  display(s1.name, s1.id, s1.marks); // for student1
  display(s2.name, s2.id, s2.marks); // for student2
  return 0;
}
void display(char *n, int i, float m) {
  printf("The student details are as follows:\n");
  printf(" Name= %s\n ID= %d\n marks= %f\n\n", n, i, m);
}

Output:-
The student details are as follows:
 Name= Rohan
 ID= 102
 marks= 55.000000

The student details are as follows:
 Name= Mohan
 ID= 103
 marks= 99.000000

But here we see that in the case of more elements inside the structure passing individual elements would increase the complexity and hence we better go for passing the entire structure itself.

b) Passing the entire structure

In the case of passing the entire structure, we have to keep in mind that if we declare the structure inside the main() method it gets localized. So we need to declare the structure outside the main() method. Here is the program that shows how we pass the entire structure to another function.

#include <stdio.h>
/*declaring the structure outside the main() function*/
struct student {
  char name[10];
  int id;
  float marks;
};
void display(
    struct student); // declaring the function to display the student details
int main() {
  struct student s1 = {"Ram", 101, 79.0}; // initializing structure
  display(s1); // sending the complete student details to the function display()
  return 0;
}
/*Function to display the student details*/
void display(
    struct student s) // formal parameter takes the entire structure as argument
{
  printf("The student details are as follows:\n");
  printf(" Name= %s\n ID= %d\n marks= %f\n\n", s.name, s.id, s.marks);
}

Output:-
The student details are as follows:
 Name= Ram
 ID= 101
 marks= 79.000000

Knowledge is most useful when liberated and shared. Share this to motivate us to keep writing such online tutorials for free and do comment if anything is missing or wrong or you need any kind of help.

Do not forget to share and Subscribe.

Keep Learning… Happy Learning.. 🙂

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index