Monday, 11 March 2019

What is the 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 to user-defined data type. In this article, we will read about the way in which we can solve the problem by using the structure in C.

Structure in C


















Define structure in C

A structure in C is a user-defined data type. A structure is a collection of variables of different data types.

Learn Data Types in C

The difference between array and structure in C 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.

What is Array in C

Create a structure in C

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



Syntax

struct name_of_structure
{
data type member1;
data type member2;
data type member3;
...............................
...............................
...............................
...............................
data type member N;
};

Example

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


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

Values cannot be defined within the structure definition.
For example
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

Structure variable in C can be declared as:

1. Structure variable along with structure definition

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

Syntax
struct name_of_structure
{
data type member1;
data type member2;
data type member3;
...............................
...............................
...............................
...............................
data type 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;
};

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

There are two operators that can access the structure members.
1. Member operator or Dot/period operator (.)

Operator in C

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 structure members can be accessed.


#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
Structure in C












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 that how much memory consumed by a normal union/structure in C, and how much memory space will be required by structure in C when using bit field.

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

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

Output
Structure in C








//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 structure with bit field = %d\n", sizeof(p1));
    return 0;
}


Output
Structure in C




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. This 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 particular 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. Bit field has only one advantage that is it reduces the consumption of memory space.













Recommended Posts

Pointer Pointer
Function pointer in C

Strings in C




















































































No comments:

Post a Comment