Class 9 | Structure and Union | C Programming Notes

STRUCTURE

 

A structure is user-defined data type that can hold multiple data of the same data types or varying data types. The data items in a structure are referred to as a member or element of the structure.

 

For eg: student_id, name, phone_number, marks_scored can be members of a structure.

 

DECLARATION OF STRUCTURE AND STRUCTURE VARIABLES

 

Structure can be declared with struct keyword. The struct statement defins a new data type with more than one member.

 

Syntax for declaring a structure

 

Struct structure_name

 

{

 

Data_type member_variable1;

 

Data_type member_variable2;

 

……………………………………………..

 

Data_type member_variable n;

 

};

 

Eg:

 

Struct employee

 

{

 

Int emp_id;

 

Char name[20];

 

Char add[20];

 

Float sal;

 

}em1, em2, em3;

 

Accessing member of structure

 

The member of a structure can be accessed using period (.) sign between the structure variable and the member.

 

Syntax

 

Structure_variable.member

 

Eg:

 

#include<stdio.h>

 

Struct student

 

{

 

Int roll;

 

Char name[20];

 

Char add[20];

 

}

 

St;

 

Void main()

 

{

 

Printf(enter name:);

 

Scanf(%s, &st.name);

 

Printf(enter roll no:);

 

Scanf(%s, &st.roll);

 

Printf(enter address:);

 

Scanf(%s, &st.add);

 

Printf(name=%s, rollno=%d, address=%s, st.name, st.roll, st.add); }

 

Nested structure

 

Nested structure in C refers to a structure within a structure. The structure provides a convenient way to declare structure as a member of another structure.

 

Syntax:

 

Struct structure_name1

 

{

 

Data_type member1;

 

Data_type member2;

 

………………………………..

 

Data_type member n;

 

}

 

 

The above structure structure_name1 can be nested within another structure struct structure_name2

 

{

 

Data_type member1;

 

Data_type member2;

 

………………………………..

 

Struct structure_name1 structure_variable1;

 

} structure_variable2;

 

ACCESSING MEMBERS OF A STRUCTURE IN A NESTED STRUCTURE

 

The members of a structure adders can accessed by;

 

Emp.add.city

 

Emp.add.street

 

Emp.add.phone

 

But the members within employee can be accessed as usual.

 

Emp.name

 

Emp.emp_id

 

Eg:

 

#include <stdio.h>

 

struct FirstName {

 

char first[20];

 

};

 

struct LastName {

 

char last[20];

 

};

 

struct FullName {

 

struct FirstName first_name;

 

struct LastName last_name;

 

};

 

int main()

 

{

 

struct FullName name;

 

printf(“Enter your first name: “);

 

scanf(“%s”, name.first_name.first);

 

printf(“Enter your last name: “);

 

printf(“Your full name is: %s %s\n”, name.first_name.first, name.last_name.last);

 

return 0;

 

}

 

ARRAR OF STRUCTURE

 

The structure provides a convenient way of creating its array. Similar to the array we create for other data types, the structure array can also by created.

 

Roll name add Roll name add Roll name add ……………………….. Roll name add
St[0] st[1] st[2] st[3] st[4]

 

The members of an array cn be accessed as:

 

St[0].roll, st[0].name, st[0].add, and so on.

 

Eg:

 

#include <stdio.h>

 

#include <string.h>

 

struct Person

 

{

 

char name[MAX_NAME_LENGTH];

 

};

 

int main()

 

{

 

struct Person people[MAX_PEOPLE];

 

int num_people;

 

printf(“Enter the number of people: “);

 

scanf(“%d”, &num_people);

 

for (int i = 0; i < num_people; i++)

 

{

 

printf(“Enter name for person %d: “, i+1);

 

scanf(“%s”, people[i].name);

 

}

 

printf(“\nNames entered:\n”);

 

for (int i = 0; i < num_people; i++)

 

{

 

printf(“Person %d: %s\n”, i+1, people[i].name);

 

}

 

return 0;

 

}

 

UNION

 

A union is similar to a structure. Like structure, a union is also a user-defined data type that can hold multiple data of the same data types or varying data types.

 

For eg: student_id, name, phone_number, marks_scored can be members os a structure.

 

DECLARATION OF UNION AND UNION VARIABLE

 

Union keyword is used to declare a union. The union statements defines a new data type with more than one member.

 

Syntax for declaring a union

 

Union union_name

 

{

 

Data_type member_variable1;

 

Data_type member_variable2;

 

………………………………………………

 

Data_type member_variable n;

 

};

 

Eg:

 

#include <stdio.h>

 

#include <string.h>

 

union Name

 

{

 

char firstName[20];

 

char lastName[20];

 

};

 

int main()

 

{

 

union Name name;

 

printf(“Enter your first name: “);

 

scanf(“%s”, name.firstName);

 

printf(“Your first name is: %s\n”, name.firstName);

 

printf(“Enter your last name: “);

 

scanf(“%s”, name.lastName);

 

printf(“Your last name is: %s\n”, name.lastName);

 

return 0;

 

}

 

Similar to structure, the union variable of a union student can be declared as union st;

 

If you have to declare multiple union variables then, it can be declared as union student st1, st2, st3; Here, st1, st2, and st3 are the union variables that can be used to access the members.

 

We also have a way of combining union definition and its variable.

 

#include <stdio.h>

 

#include <string.h>

 

union Data

 

{

 

char name[50];

 

int id;

 

};

 

int main()

 

{

 

union Data data;

 

printf(“Enter a name: “);

 

scanf(“%s”, data.name);

 

printf(“Name: %s\n”, data.name);

 

return 0;

 

}

 

ACCESSING MEMBER OF UNION

 

The member of a union can be accessed using period (.) sign between the union variable and the member.

 

Syntax:

 

Union_variable.member

 

Eg:

 

#include <stdio.h>

 

#include <string.h>

 

union Name

 

{

 

char firstName[20];

 

char lastName[20];

 

};

 

int main() {

 

union Name name;

 

printf(“Enter first name: “);

 

scanf(“%s”, name.firstName);

 

printf(“Enter last name: “);

 

scanf(“%s”, name.lastName);

 

printf(“Full name: %s %s\n”, name.firstName, name.lastName);

 

return 0;

 

}

 

Different between structure and union

 

STRUCTURE UNION
   
Struct keyword is used to define a structure. Union keyword is used to define a union.
   
Each member of structure has dedicated All the members of union share a single or
memory address. Thus, a structure variable common memory addresses. Thus, a union
occupies more memory than that of the union. variable occupies less memory than that of a
  structure.
   
The size of memory required to store a structure The size of memory required to store a union
variable is the sum of the sizes of the memory variable is same as the member that occupies
required for each members. the largest memory.
   

 

 

All the members can be accessed at a time. Only one member can be accessed at a time.
   
Syntax: Syntax:
Struct structure_name Union union_name
{ {
Data_type member_variable1; Data_type member_variable1;
Data_type member_variable2; Data_type member_variable2;
……………………………………………… ………………………………………………….
Data_type member_variable n; Data_type member_variable n;
}st; }un:
   

 

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *