INTRODUCTION
A struct in the C programming language (and many derivatives) is a record declaration that defines a physically grouped list of variables under one name in a block of memory, allowing the different variables to be accessed via a single pointer or by the struct declared name which returns the same address.
- It is a collection / set of different data types. Like int, float, char etc.
- Each variable in structure is known as a member of the structure.
- struct key word is used to define the structure.
- Structure and union both are user define function.
- Structure allocate separate memory, Union store all in one memory.
|
STRUCTURE |
UNION |
|
Use struct key word to define a structure. |
Use union key word to define a union. |
|
Every member within structure is assigned a unique memory location. |
A memory location is shared by all the data members. |
|
Changing the value of one data member will not affect other data
member in structure. |
Changing the value of one data member will affect other data member
in structure. |
|
The total size of the structure is the sum of the size of every data
member. |
Total size of the union is the size of the largest data member. |
-Example of STRUCTURE :
Code :
#include<stdio.h>
struct student
{
char *name;
int id;
int age;
}; //; semicolon is important
int main()
{
struct student s1;
s1.name="REVATI"; //we can write . OR ->(arrow)
s1.id=1234;
s1.age=20;
printf("name :%s \n",s1.name);
printf("id :%d \n",s1.id);
printf("age :%d \n",s1.age);
}
output :
name :REVATI
id :1234
age :20
No comments:
Post a Comment