Notes on Structure and Union | Grade 12 > Computer Science > Programming in C | KULLABS.COM

Structure and Union

  • Note
  • Things to remember

Introduction

A structure in C is a heterogeneous user-defined data types. A structure may contain different data types. It groups variables into a single entity.

Example of structure declaration:

struct student

{

char name [25], address [80];

int age;

float height, weight;

}

;

The above declaration of groups name, address, age, height, and weight of a person into an entity is called the student. The declaration starts with the reserved word struct, followed by the name of structure student. It follows many declarations in the curly braces (compound statement).

The declarations specify the members of the structure. The identifier student names the structure and is called the structure tag or simply tag. The identifier student is considered to be a new type defined by the user.

No memory has been allocated to the structure so far. To use this structure in program, we must define a structure variables as:

.

After declaring pers to be the type student, we can access the identifiers inside this structure. A variable of type student contains the identifiers name, address, age, height, and weight. These identifiers are accessed using the dot (.) operator.

Eg. pers.age =65;

To input data respectively we use the following convention.

scanf(“%i”, “, &pres.age);

Features of Structures

Structures handle individual elements of different type (int, float,etc.)

  • Structures contain multiple members.
  • Union also contains multiple members.
  • Union uses the same memory address.

Defining Structures

The structure definition is more complicated than array defining. The structure must be defined in terms of its individual members. In general term, a structure can be defined as Structure is a user-defined data type in C which allows you to combine different data types to store a particular type of record. Structure helps to construct a complex data type in a more meaningful way. It is somewhat similar to an Array. The only difference is that array is used to store a collection of similar data types while structure can store a collection of any type of data.

The structure is used to represent a record. Suppose you want to store a record ofStudentwhich consists of student name, address, roll number, and age. You can define a structure to hold this information.

Struct tag

{

member 1;

member 2;

……

member n…..;

}

;

Here, the storage class is optional but struct is a required keyword, the tag is the name that appeared in the structure declaration and variable 1, variable 2, variable 3, …., variable n are structure variables of type tag.

Example: (Outline of structure)

struct account

{

int acc_no;

char name[80];

float balance;

}

In this definition,

the account is the structure name

acct_no is the member of structure

acct_type is the member of structure

char name[80] is the member of the structure

float balance is the member of the structure

Structure within Structure

A structure can have another structure included within called embedded structure. The embedded structure should be declared before the declaration of the outer structure.

Example: (Structure Outline)

# First structure which is going to be embedded.

struct date

{

int month;

int day;

int year;

}

;

# Second structure which includes the first structure.

struct account

{

int acct_no;

int acct_type;

char name[80];

float balance;

struct date lastpayment;

}

oldcustomer, newcustomer;

NOTE: The account contains another structure named the date. Declaration of data proceeds declaration of account structures.

# Write a structure that manipulates the accounts of 100 members which includes the structure date.

struct date

{

int month;

int year;

}

;

# Second structure which includes the first structure will look like the one given below.

struct account

{

int acct_no;

int acct_type;

char name[80];

float balance;

struct date lastpayment;

}

customer [100];

Hence 100 element array structure is defined.

Processing Structure

The members of a structure are usually processed individually, as separate entities. A structure member can be accessed by writing in the following format.

variable.member

Hence,

variable refers to the name of structure type variable. Member refers to the name of the member within the structure.

The (.) dot separates the variable name and forms the member name. It has the highest precedence and its associative is left to right.

Write a structure to formulate and print the structure.

#include <stdio.h>

#include <conio.h>

#include <string.h>

void main ()

{

static struct

{

int integer;

float real;

}

structure = {7, 3,.14};

printf(“\n Integer = %d, real = %5.2f\n”, structure.integer, structure. real);

}

Structure members can be accessed or processed in the same manner as ordinary variables of the same data type, Single valued structure members can appear in expression, they can be passed to functions, and they can be returned from functions, as though they were ordinary single valued variables.

The DOT Operator

The DOT (.) operator is a member of the highest precedence group and associates from the left to right. Since it is the precedence of highest precedence, the dot operator will take precedence over various arithmetic, relational, logical, assignment and unary operators.

# Write a program to use structure and display various elements.

#include <stdio.h>

#include <conio.h>

#include <string.h>

void main ()

{

struct info

{

char fname[20];

char sname[20];

char address[20];

float salary;

char telephone[10];

}

;

struct info v1={“Roshan”, Tamang”, “KTM”, 890.00,”21457893”};

struct info v1={“Ajaay”, Tamang”, “KTM”, 8909.78,”45231768”};

clrscr ();

printf(“\n First name is %s, v1.fname);

printf(“\n Last name is %s, v1.sname);

printf(“\n Address is %s, v1.address);

printf(“\n Salary is %s, v1.salary);

printf(“\n Telephone is %s, v1.telephone);

printf(“\n First name is %s, v1.fname);

printf(“\n Last name is %s, v1.sname);

printf(“\n Address is %s, v1.address);

printf(“\n Salary is %s, v1.salary);

printf(“\n Telephone is %s, v1.telephone);

}

# Write a program using structure to read fname, sname, address, salary and telephone and print them.

/* structure and array */

#include <stdio.h>

#include <conio.h>

#include <string.h>

void main ()

{

int i;

struct info

{

char fname[20];

char sname[20];

char address[20];

float salary;

char telephone[10];

}

;

struct info v1[5]={“Roshan”, Tamang”, “KTM”, 890.00,”21457893”,

“shayam”, “Ghimire”, “KTM”, 7890.89, “2345678”,

“jay ram”, “Shrestha”, “KTM”, 7890.89, “2345678”,

“Bishal”, “Bishowkarma”, “KTM”, 7890.89, “2345678”,

“Shasank”, “Tamang”, “KTM”, 7890.89, “2345678”};

clrscr ();

for (i=0; i<5; i++)

{

printf(“\n First name is %s, v1[i].fname);

printf(“\n Last name is %s, v1[i].sname);

printf(“\n Address is %s, v1[i].address);

printf(“\n Salary is %s, v1[i].salary);

printf(“\n Telephone is %s, v1[i].telephone);

}

}

Creating User-defined Data types

C uses typedef keyword for defining user-defined type of data in the program. The type is defined in the following fashion.

Example:

typedef struct

{

int acct_no;

int acct_type;

int name[80];

float balance;

}

record;

record oldcustomer, newcustomer;

In this example, record is defined as user defined data type. The declaration in the second line tells us that oldcustomer and newcustomer are structure variables of type record.

Unions

A union is a data type in C which allows the overlay of more than one variable in the same memory. it is often found that some variables used in the program are used only in a small portion of the source code. The union construct provides a means of which the memory space can be shared.

Declaring Union

The declaration of union is similar to the declaration of structure.

Syntax:

union[ <union type name >]

{

<type> <variable names>;

………..

}

[<union variables>];

All variables inside a union share storage space. The compiler allocates sufficient storage for the union variables to accommodate the largest element in the union.

# Write a program using structure and union construct to display the size of memory occupied by structure and union respectively.

#include <stdio.h>

#include <conio.h>

struct

{

char name [20];

int idno;

float salary;

}

emp;

union

{

char name [20];

int idno;

float salary;

}

desc;

void main ()

{

printf(“\n The size of structure is %d”, sizeof (emp));

printf(“\n The size of the union is %d”, sizeof (desc));

}

# Write a program to use union and print the members within it.

#include <stdio.h>

#include <conio.h>

#include <string.h>

union

{

char name [20];

int idno;

float salary;

}

desc;

void main ()

{

strcpy (desc.name, “Mohan”);

printf(“\n Employee Details \n \n”);

printf(“\n The name is %s”, desc.name);

desc.idno = 100;

desc.salary = 6500.00;

printf(“\n The id no is %d”, desc.idno);

printf(“\n The salary is %5.2f”, desc.salary);

}

(Khanal)

Bibliography

Khanal, R.C. Khanal, R.C. Computer Concept for XII. Pashupatigriha Marga, Thapathali, Kathmandu, Nepal: Ekta Books Distributors Pvt. Ltd., 2010. 269-279.



  • The data items in a structure are called members of the structure. The struct keyword is used to start a structure declaration or a structure variable definition.
  • The dot (.) operator is used to separate structure name and a member using a pointer to the structure.
  • The union keyword is used to specify union data type in the union declaration or union variable definition. Union helps to access the same memory location with different union members.
  • A structure is a derived data type to organize a group of related data items of different data types. A group of structures may be organized in an array resulting in array of structures. Each element in the array is a structures.

 

.

Very Short Questions

0%

ASK ANY QUESTION ON Structure and Union

No discussion on this note yet. Be first to comment on this note