Monday, 11 March 2019

What is the structure in C?

structure in c


The C programming language has a fundamental and user-defined data type. But they can not solve the problems of our real world. For a lot of situations, we need a custom data type. Suppose you want to record the details of 100 students such as their name, their roll number, their age, and their marks. Then you have to create four variables of size 100 i.e. name[100], roll_number[100], age[100] and marks[100]. To record details of the student using these four variables may seem very easy to see, but actually, it is complicated.  To solve this problem, we need a user-defined data type. In this article, we will read about how we can solve the problem by using the structure in C. 

Define structure in C

C structure is a user-defined data type. A structure is a collection of variables of different data types (like int, float, char, double).

See Also: Union in C

The difference between array and structure is that array is a collection of the data item of the same data type, but the structure is a collection of the different data type in C.

Create a structure in C

struct keyword is used to define the structure. It is a reserved word in C compiler.



Syntax

struct name_of_structure
{
datatype member1;
datatype member2;
datatype member3;
...............................
...............................
...............................
...............................
datatype member N;
};

Example

struct details_of_student
{
char name[100];
int roll_number;
int age;
float marks;
};


structure in c

Structure definition should be terminated with the semicolon (;).

See Also: C Tokens (Special Symbols like Brackets, semicolon)

Values cannot be defined within the structure definition.
For instance, 
struct details_of student
{
char name[100]="Sharon";
int roll_number= 45;
int age = 14;
float marks = 986;
};

Structure in C can be defined as a global scope or local scope.

Declare structure variable in C

Two ways to declare a structure variable in C,

1. Structure variable along with structure definition

Structure variable can be declared before the ending of a structure definition.

Syntax
struct name_of_structure
{
datatype member1;
datatype member2;
datatype member3;
...............................
...............................
...............................
...............................
datatype member N;
}Structure variable;

Example
struct details_of student
{
char name[100];
int roll_number;
int age;
float marks;

}details;

2.  Structure variable after structure definition

We can declare the structure variable anywhere in the program after the structure definition based on the structure scope.

Syntax
struct Name_of structure structure_variable;

Example
struct details_of student
{
char name[100];
int roll_number;
int age;
float marks;

};

//Declare structure variable
struct details_of_strudent details;


Initialize structure members

Structured members cannot be initialized with the declaration.

Example
struct student
{
char name[100] = "King";
int roll_number = 45;
float marks = 456;
};

The output would be COMPILER ERROR: cannot initialize member here.

When we declared variable, then no memory is allocated to it. Memory is allocated when the variables are initialized.

struct data
{
int roll_number;
float marks;
};

int main ( )
{
struct data p = { 56, 589};
}

Access structure members

 Two operators can access the structure members.
1. Member operator or Dot/period operator (.)


Syntax
structure_variable.name_of_member;

2. Structure pointer operator or Arrow operator (->)

Syntax
pointer_to_structure->member_name;


Here, we are discussing a program that will explain the declaration of structure, initialization of structure, and how to access structure members.


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//Structure definition
struct details_of_student
{
    char name[100];
    int roll_number;
    int age;
    float marks;
};
int main ()
{
struct details_of_student A = {"King",10,15,475}; //Simple structure variable A
struct details_of_student *ptr; //Pointer to details of student;
ptr =&A;

printf (" Name of student = %s\n Roll number of student =%d\n Age of student = %d\n Marks of student = %f\n\n\n",A.name, A.roll_number,A.age,A.marks);

printf (" Name of student %s\n Roll number of student %d\n Age of student %d\n Marks of student %f\n\n\n",ptr->name, ptr->roll_number, ptr->age, ptr->marks);

return 0;

}

Output
Name of student = King
Roll number of student = 10
Age of student = 15
Marks of student = 475.000000

Name of student = King
Roll Number of student 10
Age of student 15
Marks of student 475.000000

Program for array of structure in C

#include<stdio.h>
struct Analysis
{
   int a, b;
};

int main()
{
   // Create an array of structures
   struct Analysis array[10];

   // Access array members
   array[0].a = 120;
   array[0].b = 130;

   printf("%d %d", array[0].a, array[0].b);
   return 0;
}

Output
120 130


A bit field in C

A bit field in C is used to optimize memory usage. 

Syntax
struct
{
data-type member : size_of_bits;
};

Let us understand with the help of an example, how much memory consumed by a normal union/structure in C, and how much memory space will be required by the structure when using bit field.

#include <stdio.h>
#include <string.h>
//Structure definition
struct
{
    int a;
    int b;
}p1;

int main()
{
    printf ("Size of the normal structure  is %d\n", sizeof(p1));
    return 0;
}

Output
Size of the normal structure is 8





//C program demonstrate working of structure with bit field.
#include <stdio.h>
#include <string.h>

//Structure definition
struct
{
    int a : 1;
    int b : 1;
}p1;

int main()
{
    printf ("Size of the structure with bit field = %d\n", sizeof(p1));
    return 0;
}


Output
Size of the structure with bitfield = 4


After seeing at the output of these two programs, the question arises why this happened? The member 'a' and 'b' having the value either zero or one. It is not going to have all integers restrict the size of this to a particular number of bits. So, rather than giving 4 bytes of integer which happened in this specific case, for this 'a' and 'b' integer digit 1 is reserved. We will see that there is 4 byte only. So, up to 32-bit, no issues will occur. If the value of a is 16 and b is 17 then the total number of bits is 33, it will go to the next level it will become 8 bytes. Bitfield has only one advantage that is it reduces the consumption of memory space.


No comments:

Post a Comment