Pages

Saturday, 7 March 2020

Structure,typedef and Union in C

Structure : The structure is a user-defined data type in C, which is used to store a collection of different kinds of data.The structure is something similar to an array; the only difference is array is used to store the same data types.

struct keyword is used to declare the structure in C.Variables inside the structure are called members of the structure.

Syntax:
struct structure_name
{
  data-type member-1;
  data-type member-2;
  data-type member-3;
  data-type member-4;
}; 
Example:
struct Person        
{
    char name[50];
    int a;
    float salary;
};

Declaring Structure variables separately

struct car
{
    char name[20];
    int price;
    char company[10];
};
struct car c1, c2;   //declaring variables of struct car

Declaring Structure variables with structure definition

struct car
{
    char name[20];
    int age;
    char company[10];
}c1, c2;

Accessing Structure Members : There are two types of operators used for accessing members of a structure.
 Dot(.) - Member operator
 -> - Structure pointer operator
Example:     c1.age;
Structure as Function Arguments
Example:
#include<stdio.h>
struct Student
{
    char name[10];
int roll;
};
void show(struct Student st);
void main()
{
    struct Student std;
    printf("\nEnter Student record:\n");
    printf("\nStudent name:\t");
    scanf("%s", std.name);
    printf("\nEnter Student rollno.:\t");
    scanf("%d", &std.roll);
show(std);
}
void show(struct Student st)
{
    printf("\nstudent name is %s", st.name);
printf("\nroll is %d", st.roll);
}

Pointers to Structures
Syntax:
struct structure_name
{
  data-type member-1;
  data-type member-1;
  data-type member-1;
  data-type member-1;
};
main()
{
  struct structure_name *ptr;
}

Example:
#include <stdio.h>
int main()
{
  struct student
  {
    char name[30];
    int roll_no;
  };
  struct student stud = {"rihana",1};
  struct student *ptr;
  ptr = &stud;
  printf("%s %d\n",stud.name,stud.roll_no);
  printf("%s %d\n",ptr->name,ptr->roll_no); //access of struct            element using ->
  return 0;
}

typedef : typedef is a keyword used in C language to assign alternative names to existing datatypes
Syntax:
typedef <existing_datatype_name> <alias_datatype_name>

Example:      typedef signed long slong;

Now the thing is this 'slong', which is an user-defined identifier can be implemented in program for defining any signed long variable type within program.

Example:     slong g, d;

Application of typedef : typedef can be used to give a name to user defined data type as well. 

Lets see its use with structures.
typedef struct
 {     
 type first_member;
type second_member;
type thrid_member;
} type_name;

this type_name can be used to declare a variable of this stucture type.
type_name t1, t2;

Structure definition using typedef : Let's take a simple example to understand how we can define a structure in C using typedef keyword.

Example:
#include<stdio.h>
#include<string.h>
typedef struct student
{
    char name[50];
int roll_no;
}stu;
void main()
{
    std s1;
    printf("\n Enter Student record:\n");
    printf("\n Student name:\t");
    scanf("%s", s1.name);
    printf("\n Enter Student roll_no: \t");
    scanf("%d", &s1.roll_no);
    printf("\n Student name is %s", s1.name);
    printf("\n Roll_no is %d", s1.roll_no);
}
Using typedef with pointers : typedef can be implemented for providing a pseudo name to pointer variables as well. 
Example:    int* a;
The binding of pointer (*) is done to the right here.
Example:
typedef int* ptr;
ptr x, y, z;

Unions : Unions are conceptually similar to structures.Unions are user-defined data type, which is used to store a collection of different kinds of data, just like a structure.

Syntax:
union unionName
 {
     //member definitions
 };
Example:
union Employee 
{
char name[50];
float salary[50];
int emp_id;
};

Creating union variables 
Example:
union Employee 
{
char name[50];
float salary[50];
int emp_id;
}emp1,emp2,*emp3;

Or
Example:
union Employee 
{
char name[50];
float salary[50];
int emp_id;
};
int main()
{
  union Employee emp1, emp2,*emp3;
  return 0;
}

Access members of a union : We use the dot(.) operator to access members of a union. To access pointer variables, we use also use the --> operator.

To access name         emp1.name;
To access salary        emp2.salary;
To access emp_id     (*emp3).emp_id; or emp3-->emp_id;

Difference between structure and union

Basis of comparison

Structure

Union

Basic

The separate memory location is allotted to each member of the 'structure'.

All members of the 'union' share the same memory location.

Declaration

struct st_name{
type element1;
type element2;
.
.
} variable1, variable2, ...;

union u_name{
type element1;
type element2;
.
.
} variable1, variable2, ...;

keyword

'struct'

'union'

Size

Size of Structure= sum of size of all the data members.

Size of Union=size of the largest members.

Store Value

Stores distinct values for all the members.

Stores same value for all the members.

At a Time

A 'structure' stores multiple values, of the different members, of the 'structure'.

A 'union' stores a single value at a time for all members.

Way of Viewing

Provide single way to view each memory location.

Provide multiple way to to view same memory location.

No comments:

Post a Comment

Program for Largest Number among three numbers

Program for Largest Number among three numbers /* c program to find largest number among 3 numbers*/ #include <stdio.h>   in...