Thursday 5 December 2019

C Tutorials - Structures with Examples


Structures

Introduction

The C language allows us to create custom data types.

The structure is a custom data type which c combines different data types .

The structure is a custom data type which combine different data types to form a new user define data type.

Definition

A structure is a collection of variable reference under one name providing a convincible means of related information together.

Format: struct tag_name
{
data _type member1;
data_type member2;
-------------------
---------------------
};

here a keyboard struct declares a structes to hold the details of field of different data types.

Example:
struct addr
{
char name [30];
char city [15];
int pincode ;
};

Creating Structure variable

structure can be created in two ways:

1. declaration using tagname anywhere in the program.

Example:

struct book
{
char name [30];
char author [25];
float price;
};
struct book book1 book2

2. it is also allowed to combine structure declaration and variable declaration in one statement.

Example:

struct person
{
char *name;
int age;
char*address;
};
p1,p2,p3

while declaring structure variable along with their definition, the use of tag-name is optional.

Struct
{
char *name;
int age;
char * address;
}
p1,p2,p3


Giving values to member

The link between a member and a variable is established using member operator `.' to dot operator.


Structure Initialization

a structure variable can be initialization as any other data type.

Main()
{
static struct
{
int weight;
float height;
}
}
student{560,080,75};

This assign the value 60 to student weight and 180.75 student height. there is a one to one correspondents between the members and their initializing values.

The following statements initialize two structures variables:

Main()
Struct st_decord
{
int weight;
float height;
}
static struct st_record student2={53, 170,60}
}

another method is to initlialize a structure variable outside the function.

Struct st_record/* No static word*/
{
int weight;
int height
}
student={60,50,75}
}
main()
{
static struct st_record student2={53,170,60}
}

Comparison of structure variables

Two variables of the same structure type can be compared the same way as ordinary variables.

operation meaning

person1=person2*assign perosn2 to person1

person1== person2*compare all name of person1 and person2 and return1



Arrays of structures

The most common use of structures is in arrays of structures. To declare an array of structures, first the structure is defined then an array variable of that structure is declared.

E.g.: struct class student [100];

It defines an array called student which consists of 100 elements of structure named class.

Ans is stored inside the memory in the same way as a multidimensional array example program.

To implements on array of structures.

Arrays with in structures

Single as multidimensional arrays of type int as float can be defined as structure members.
Example:
struct marks
{
int number;
float subject[3];
}
student [2];

Here the member subject contains three elements, subject[0], subject[1] and subject[2] there elements can be accessed using appropriate subscript.

For instance, the name student [1] student [2]; would refer to the marks obtained in the third subject by the secured student.

Structures with in structures

Structures within a structure means nesting of structures.

Example:

struct salary
{
char name [20];
char department [10];
int basic-pay;
int dearness-allowance;
int huse_rent_allowance;
int city_allowance;
}
employee;

This structure defines name, department, basic pay and three kinds of allowances.

All the items related to allowance can be grouped together and declared under a sub-stricture. As shown below, strut salary

{
char name []2;
char department [10];
struct;
}
int dearness;
int house_rent;
int city;
[allowance;
}
employee's;

The salary structure contains a member named allowance which use is a structures with.

Three members. Now ; the member compared in the inner structure;, namely, ;dearness,house_rent and city can ;be left to as;

employee. ;allowance. Dearness
employee. Allowance. House_rent
employee allowance. city

The inner most member in a nested structure can be accessed by chaining all the concerned structure variables (from outermost to inner most) with the member using dot operator.


Passing structure to function

There are three methods by which the values of structure can be transferred from one function to another:

1. The first method is to pass each member of the structure as an actual argument of the function call.

The actual argument is then treated independently like ordinary variables.

2. The second methods involve passing of a copy of the entire structure to the called function

Since the function is working on a copy of the entire structure to the called function, changes are not reflected in the original structure (in the calling function).

It is necessary for the entire function to return the entire structure back to the calling function.

3. The third approach employs a concept called pointers to pass the structure as an argument .

In this case, the address location of the structure is passed to the called function.

The function can access indirectly the entire structure and work on it.

The general format of sending a copy of structure to the called function is:

function_name (structure_variable_name)



Previous Page                                       Next Page

More Topics:














100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...