Structures in C

Until now, In every C Program, we have used a lot of data types to build our code like the primitive, non-primitive and even some User-defined data types like strings and arrays. Here, the case was we were storing values of the same data type like in arrays all the values were either integers, floats, character values etc. And same was the case in Strings, all char values. But what if we were to store values of different types together as a single unit or we can say a different Data-type. In such conditions , the concepts of arrays and strings would fail. Here we need something more dynamic. Here - Structures come into play.


What are Structures in C Programming?

Structure is also a User-defined Data type in C programming. It helps us in grouping variables of different data types under a same name. These are actually collection of related variables in a single unit. Structures are used to represent a record. It can be of anything like a record of students in a classroom, it actually creates a data type which can be used in such cases where the data of each student is same. 

For example, every student record has a roll number, father's name, mother's name , admission number etc. These can be grouped in a structure so that we can operate on them as a unit, which is more logical in this case

Declaring a C Structure!

Structures can be declare using the following syntax :

struct structure_name 
{
  type member1;
  type member2;
  type member3;
  .
  .
  .
} structure_variables;

* Here struct is a keyword. And also don't forget to add the semicolon at the end of the curly brackets.

Example:

struct student
{
   char name[20];     // 20 bytes of memory
   char address[50]; // 50 bytes of memory
   int roll_no;          // 4 bytes of memory

}s1,s2;

  •  Here the name of the structure is ' student ' , it contains 3 variables - name, address and roll_no. Now if we declare any structure variable of the structure student, then each variable will have its own name, address and roll_no variables. 
  • Here s1 and s2 are the structure variables in this case. 
  • The total size of the structure is 74 bytes , i.e , the sum of sizes of all the variables in the structure.
  • Each object of the structure will occupy 74 bytes of memory.

Initializing a structure variable

You cannot initialize a structure member within the structure only. You need to do this in main() function. The members of the structure are accessed using the period(.) operator.

#include<stdio.h>
#include<string.h>
struct student
{
   char name[20];
   char address[50];
   int roll_no;

}s1;
int main() 
{
      /* We can also declare a variable here also using
            struct student s1 ;
       struct keyword is mandatory here
      */
      strcpy(s1.name, "Coder");
      strcpy(s1.address, "Chandigarh, Punjab");
      s1.roll_no=1;
      printf("Values of s1 are: \n");
      printf("Name : %s\n",s1.name);
      printf("Address : %s\n",s1.address);
      printf("Roll Number : %d\n",s1.roll_no);
      
      return 0;
}

OUTPUT 
Values of s1 are:
Name : Coder
Address : Chandigarh, Punjab
Roll Number : 1


* If we try to print the value of any structure member without initialising it. Then it would be Zero.

struct student
{
   char name[20];
   char address[50];
   int roll_no;

};
int main() 
{
      struct student s1;
      printf("Roll Number : %d\n",s1.roll_no);
      
      return 0;
}

OUTPUT 

Roll Number : 0

* If we print a string member of the structure without any initialization, then unknown symbols would be printed on the screen.

Program 1. Add 2 distances together using structures.

#include <stdio.h>
struct distance
{
    int feet;
    float inch;
}

int main()
{   struct distance d1 , d2 , sum;
    printf("Enter 1st distance\n");
    printf("Enter feet: ");
    scanf("%d", &d1.feet);
    printf("Enter inch: ");
    scanf("%f", &d1.inch);

    printf("Enter 2nd distance\n");
    printf("Enter feet: ");
    scanf("%d", &d2.feet);
    printf("Enter inch: ");
    scanf("%f", &d2.inch);

    
    sum.feet = d1.feet + d2.feet;
    sum.inch = d1.inch + d2.inch;

    while (sum.inch >= 12
    {
        ++sum.feet;
        sum.inch = sum.inch - 12;
    }

    printf("Sum of distances = %d\'-%.1f\"", sum.feet, sum.inch);
    return 0;
}

OUTPUT

Enter 1st distance
Enter feet: 12
Enter inch: 7.9
Enter 2nd distance
Enter feet: 1
Enter inch: 9.7
Sum of distances = 14'-5.6"

Limitations of Structures

  • We cannot use arithmetic operators on the structure variables. It will show an error like struct student s1 and s2 are 2 structure variables, if we do s1 + s2, it will give an error.
  • They do not permit Data Hiding. Can be accessed from anywhere, any function.
  • We cannot create functions inside the structures.
  • Not as powerful as the OOPs features of C++ or Java.
Ritish

Just a novice blogger

Post a Comment (0)
Previous Post Next Post