Search This Blog

Friday, April 24, 2020

Structure and Union full explaination in C Programming

           C-Programming:-

In this tutorials you will learn:-

 Structure :-

 

It is a collection of different data type variable group together under a single name.

Syntax :-

struct  structure name 
{
data type  mem 1 ;
data type  mem 2 ;
data type  mem 3 ;
, , , ,  , , ,    , , , , , , 
, , , ,  , , ,    , , , , , , 
data type  mem n;

 Declaration of structure:-

struct book 
{
int pages ;
char name [ 100 ] ;
float price ;
} b ;

There are 3 types of declaration of structure :-

1. struct book 
{
int pages ;
char name [ 100 ] ;
float price ;
} ;
 struct book b ;

2. Struct book 
{
int pages ;
char name [ 100 ] ;
float price ;
} b ;

3. struct 
{
int pages ;
char [ 100 ] ;
float ;
} b ;

 Initializing and accessing of declaration :-

The link between a member and a structure variable is establish using member operator and dot operator. 
Struct book 
{
int pages ;
char name [ 100 ] ;
float price ;
} b = {   100    ,  codeswithabhi  ,  1000.234  } ;
             page            name                price      

By using member operator :-

struct book 
{
int pages ;
char name [ 30 ];
float price ;
};
struct book b;
b.pages = 100 ;
strcpy ( b. name , : codeswithabhi");
b.price = 345.32; 
so here we see the use of member operator in structure 

Let see one example of structure for understand every point ..

#include<stdio.h>
struct book 
{
int pages ;
char name [ 100 ] ;
float price ;
};
int main (  )
{
struct book  b ;
printf("enter pages , book name , price of book");
scanf("%d%s%f ", &b.pages , b.name , &b.price);
printf("\n details of books are ");
printf(" pages of book are = %d", b.pages);
printf("writer name = %s", b.name);
printf(" price of book = %d",b.price);
return ( 0 );
}

OUTPUT

enter pages , writer name , price of books  1600
codeswithabhi
234.23
 details of book are 
pages of books = 1600
write name  =  codeswithabhi
price of books = 234.23

Array of structure :-

Array of structure means collection of structure array storing different type of structure variable 

For example :-

#include<stdio.h>
struct result 
{
int rollno ;
char name [ 30 ] ;
float marks ;
};
  int main ( )
{
struct result r [ 60 ] ;
int i ;
printf("enter details of student ");
for (i=1 ; i<=60 ; i++)
{
printf("\n enter rollno , name , marks ");
scanf("%d", & r[i].rollno);
scanf("%s", r[i].name );
scanf("%f", & r[i].price);
}
for(i=1 ; i<=60 ; i++)
{
 printf("%d\n",name );
printf('%s\n", rollno);
printf("%f\n",marks);
}
return( 0 ) ;
}

OUTPUT 

enter details 
enter roll no , name , marks 
1
Abhijeet
90.4 
it will be continued up to 60 ....

Pointer to structure :-

 We have already learned that a pointer is a variable which points to the address of another variable of any data type . Similarly we can have a pointer to structure , where a pointer variable can be points to the address of a structure variable. 

Syntax 

struct tag name 
{
member 1 ;
member 2 ;
};
struct tag name  *struct name ;

For example :-

#include<stdio.h>
struct name 
{
int a ;
float b;
};
int main ( )
{
struct name *ptr, p;
ptr = &p;
printf("enter integer no : ");
scanf("%d\n",&(*ptr).a);
printf("enter float value");
scanf("%f\n",  &(*ptr).b);
printf("displaying: ");
printf("%d \ n %/f ", (*ptr).a , (*ptr).b);
return( 0 );
}

OUTPUT

enter integer no 10
enter float value 10.5
a = 10
b = 10.5

Nested structure :-

A structure that contain another structure as a member of a variable is known as nested structure .

For example:-

#include<stdio.h>
int main ()
{
struct address 
{
char city [30];
 char colony [30];
int pin ;
};
struct employ 
{
char name [30];
struct address a;
};
struct employ e = { "abhi" , "kanpur" , "abc" , "208011" } ;
printf("%s\n%s", e.a.colony , e.a.city);
printf("%d\n %s", e.a.pi , e.name );
return( 0 );
}

OUTPUT 

abhi
kanpur 
abc
208011

 Difference between array and structure:-


                       Array

                            Structure

 1

 An array behave like a built in data type all we have to do this to declare an array variable and use it.
 First we have to design and declare a data type structure before the variable are declare and use.

 2

An array is the collection of similar data type.
 A structure have a collection of different data type.

 3

An array is a derived data type,.
 A structure is a user defined data type.


  Union :-

* Union is user defined data type just like structure.
* Each member  of structure is assigned it's own unique storage area whereas union all the member share common storage area.
* All member share  common area so only one member can be active at a time.
* Union are used when all the member are not assigned value at the same time.

syntax:-

Union tag name 
{
data type  member 1;
data type  member 2 ;
 };

There are 3 types of declaration of union in C are :-

1.
 union sample 
{
int a;
float b;
char c;
};
union sample s;

2
union  sample 
[
int a ;
float b;
 char c;
}s;

3. 
union 
{
int a ;
float b;
char  c;
}s;

Initialization and accessing of union :-

Union sample
{
int a;
float b;
char c;
};
int main ( )  
{
union sample s ={ 10 , 10.5 , 'a' };
printf('%d \ n ", s.a);
printf('%f  \ n", s.b);
printf('%c \ n ", s.c);
return( 0 );
}

OUTPUT

10
10.5
a

Memory status of union and structure :-

1. Union's memory status:-

declare one union--
union sample 
{
int a;
float b;
char c;
} s;
         ----------sample---------





 -------------int a ------
---------------------------float b ----------------
--char c---
As we know that a union share common storage area.
so here, int a takes 2 bytes in memory.
float b takes 4 bytes 
char c 1 byte
so total bytes use during the program is =  4 bytes .

2. Structure:-

Structure's memory status:-

declare one srtucture  -
 struct sample 
{
int a;
float b;
char c;
} s;
 -----sample-----



---------int a ---------





 --- ------------------float b ---------------------------


---char c ---
As we know that the structure's member occupy separate storage area.
so here int a takes separate 2 bytes 
float b takes separate 4 bytes
and char c is separate 1 byte in memory 
then, the total space require by structure is 7 bytes.

 Difference between union and structure :-


                   Union

                          Structure

 1.

 All member share the same storage area.
Each member is assigned it's own unique storage area .

 2.

Maximum memory required by the member is allocated.
Total memory required by all member is allocated.

 3.

Only one member is active at a time
All member is active at a time.

 4.

Only first member can be initialized
All member can be initialized .

 5.

Require less memory.
Require more memory.

Thanks for reading..........


Mr. abhijeet dwivedi

Developer

Hey, I'm Abhijeet

0 comments:

Post a Comment

If you have any doubt, please let me know