Structures in C Programming Language(Basics)- Declaration and accessing

The array allows us to store multiple data of the same data type under the same name. What if we need to store multiple data of different data type under the same name? No problem, C Programming language allows us to create structures which let us store multiple data belonging to different data types under the same name.

Structures are mostly used for maintaining records like mark sheets with student details like name, ID number, and marks stored under the same name.

Another example is for maintaining library records with the name of the book, author, book ID, and subject stored under the same name. This is how data gets stored in a structure:

Structures

1. Advantages of Structures over Arrays

To begin with, structures can handle multiple data types that are not possible in arrays. Arrays can only handle the same type of data. Consider the example stated above of the students’ details.

We can make use of the array in order to store and access the name, ID number, and marks but we lose a particular identity relating the data to only one student.

In the case of structures, this is not a problem. We can store and access the data relating to one student without losing the particular identity even with an enormous amount of data.


2. Declaring a structure

We already know that structure falls under user-defined data types.  So we define a new data type using the keyword “struct“.

The syntax of the declaration is given below:

struct structure_name // give any name to your structure
{
  datatype1 element1;
  datatype2 element2;
  datatype3 element3;
};

Now we can define variables that shall contain the elements declared under “struct“.

This is shown below:

struct structure_name //Give any name to your structure
{
  datatype1 element1;
  datatype2 element2;
  datatype3 element3;
};
struct structure_name variable_declarations;

Let us consider the student example:

struct student
{
  char name[10];
  int ID;
  float marks;
};
struct student s1,s2,s3;

Note:

  • The declaration of a structure only defines the form of the structure and not allocates memory for storage. The memory is allocated after defining the variables.
    Eg: s1,s2,s3;
  • The declaration of a structure should be followed by a semicolon(;).
  • If we initialize a structure variable to {0} then all its elements will be initialized with a value 0.
    Eg: struct student s1={0};
  • Structures are declared before any function or variable is defined. For complex codes, they are put in another file and called in the program as header files using #include.

3. Accessing Structure elements

There are two ways to access the structure elements –

3.1. Using Dot(.) operator

In an array, we use the subscript to access the different members of the array. In structures, we use the dot operator (.) also known as the member access operator.

This operator is placed between the structure variable name and the structure element that we need to access.

For example: if we need to access the name of the first student then we call it in the following format: s1.name

Similarly, for id: s1.id

Printing these values:

3.1.1. Program to store and display the student details using Structure and Dot operator

#include <stdio.h>
int main() {
  struct student // declaring the structure with name student
  {
    char name[10]; // stores the name of the student
    int id;        // stores the ID of the student
    float marks;   // stores the marks of the student
  };

  /*initializing values to all the structure variables*/
  struct student s1 = {"Ram", 101, 79.0};
  struct student s2 = {"Mohan", 102, 99.0};
  struct student s3 = {"Rohan", 103, 55.0};
  struct student s4 = {0}; // all values initialized as 0

  /*displaying student details*/
  printf("Details of student1:\n");
  printf(" Name= %s\n ID= %d\n marks= %f\n\n", s1.name, s1.id, s1.marks);
  printf("Details of student2:\n");
  printf(" Name= %s\n ID= %d\n marks= %f\n\n", s2.name, s2.id, s2.marks);
  printf("Details of student3:\n");
  printf(" Name= %s\n ID= %d\n marks= %f\n\n", s3.name, s3.id, s3.marks);
  printf("Details of student4:\n");
  printf(" Name= %s\n ID= %d\n marks= %f\n\n", s4.name, s4.id, s4.marks);
  return 0;
}

Output:-

Details of student1:
 Name= Ram
 ID= 101
 marks= 79.000000

Details of student2:
 Name= Mohan
 ID= 102
 marks= 99.000000

Details of student3:
 Name= Rohan
 ID= 103
 marks= 55.000000

Details of student4:
 Name= 
 ID= 0
 marks= 0.000000

3.2. Using arrow(->) operator

Another way to access the structure is using the arrow operator ( -> operator). This is mostly used in case of structure pointers or when we pass the address of a structure to a function(call by reference).

This operator is used in the same way as the dot operator, i.e., between the structure pointer name and structure element.

Here is an example of how we use an arrow operator (This program only discuss about how to access a value of a structure using a pointer but if you have to create a structure based on user value then you have to use memory allocation using malloc or any other related method along with scanf() to read the values for the declared structure pointer):

3.2.1. Program to store and display the student details using Structure and Arrow operator

#include <stdio.h>

// declaring the structure
struct student {
  char name[10];
  int id;
  double marks;
};

void display(
    // declaring the function to display the contents of the
    // structure address passed to the function
    struct student *);

int main() {
  struct student s1 = {"Ram", 101, 79.0};
  struct student s2 = {"Mohan", 102, 99.0};
  struct student *x;
  x = &s1; // storing the address of the structure variable s1

  /*displaying the details of student1 using pointers*/
  printf("The student details are as follows:\n");
  printf(" Name= %s\n ID= %d\n marks= %f\n\n", x->name, x->id, x->marks);

  // sending the address of the structure variable s2 to function
  // display()
  display(&s2);

  return 0;
}

/**
 * displaying the contents of the address sent to the function
 */
void display(struct student *s) {
  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

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

Note :- Click here to read more about various features of a structure in C Programming Language



Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

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