Introduction to Structures in C
This is a very important concept while Learning C Programming. Pointers and arrays are inextricably connected. In addition, in many cases, the pointers and arrays are interchangeable.
For example, by using either pointer arithmetic or array style indexing, a pointer pointing to the beginning of an array may access the array Consider the program which follows.
What is Structure in C?
Structure is user define datatype, using structure we can define a datatype that holds more than one element of different datatypes.
In short, Structure is a collection of the element of different datatypes.
How to define structure?
For example:
You want to store some information about a person: his/her name, citizenship number, and salary. You can easily create different variables name, citNo, salary to store this information separately.
However, in the future, you would want to store information about multiple persons. Now, you’d need to create different variables for each information per person: name1, citNo1, salary1, name2, citNo2, salary2, etc.
Basic Syntax for Structure
{
data-type ele 1;
data-type ele 2;
Data-type ele n;
};
Example:
struct Person
{
char name [50];
int age;
float salary;
};
How to use an Array and Structure?
Use of Array:
int arr[5]= {10,20,30,40,50};
char srt[5]= “four”;
array only stores homogenous types of elements, but using array we can’t store employee dataset or student dataset.
Array only stores the same kind of data type like int, float, char, etc.
Ex:- arr[5]={a,b,c,d,e};
Use of Structure:
Structure must be defined first in their data format, which can be used later to store several structure variables.
Consider a student database consisting of stud-name, stud-age, stud-weight, stud-phno, we can define a structure to hold this info like.
struct stud-details
{
Char name[20];
Int age;
float weight;
longint phno;
};
The keyword struct holds four details namely name, age, weight, ph-no. These are called structure elements. Each element can be a different data-type.
Here stud-details are called structure tags. Actually, it is describing a format, i.e template to represent the information.
name array of 20 char
age int
weight float
ph-no long int
Things you must remember when you are using structure:
- The template will be ended by a semi-colon(;).
- After assigning each member corresponding data-type there will be a semi-colon(;), as they all are independent statements.
- The tag name such as stud-details can be used to declare structure variables further in the program.
- The minimum size of the structure is one byte and the maximum is the sum of all member variable’s size.
- We can’t assign an empty structure in c.
Some basic difference between structure and array:
Array | Structure |
1) Array is a collection of the homogenous data type. | 1) Structure refers to a heterogeneous data type. |
2) Array uses “[ ]” for element access. | 2) Structure uses “.” For element access. |
3) Array object installation is not possible. | 3) Structure object installation is possible. |
4) Array is the primary datatype. | 4) Structure is user-defined datatype. |
5) Array data usually access using an index. | 5) Structure elements are accessed using the operator. |
6) To access the array element we need less time. | 6) To access the structure element we need more time.How to create Structures with Array?Structures can be mostly used with arrays because we have to access data in the form of partitions. Sample program to work with Structure with Array
Output on screen:1st distance Further visit: Recursion In C: All-inclusive Just In 5 Minutes How to pass Structures into Functions?This is a very important concept while Learning C Programming. A structure variable can be passed to a function in a similar way as a normal argument. Consider this example:
Output on screen:Id is: 56747 |