Write about Structures in C languages ?


Arrays can be used to represent a group of data items that belong to the same type, such as int or float.

Array is not sufficient for representing a collection of data items of diff types using a simple name.

Structure:

C supports a constructed data type known as structure. A structure is a convenient tool for handling a group of logically related data items.

For example It can be used to represented a set of attributes, such as student name, roll no and marks.

Structures help to organize complex data in a move meaningful way.

Defining a Structure:
How to declare, initialize and access structure members?

Structure must be defined first for their format that may be used later to declare structure vars.

Ex: consider a book database consisting of book name, author, number of pages and price.

structure book_bank
{
char title [20];
char author [15];
int pages;
float price;
};

The keyword struct declares a structure to hold the details of four data fields namely title, author, pages and price.

These fields are called structure elements (or) members. Each member may belong to a different type of data.

book_bank is the name of the structure. And is called the structure tag.

The above definition has not declared any variables. It simply declares a format called template to represent information As shown below.

Title                 array of 20 characters
Author            array of 15 characters
Pages             integer
Price               float

The general format of a structure definition is:
 struct tag_name
{
data type member 1,
data type member 2,
………………………
………………………
};

In defining a structure you may note the following Syntax.

1. The template is terminated with a semicolon.

2. While the entire definition is considered as a statement each member is declared independently for its name and type in a separate statement inside the template.

3. The tag name such as book_bank can be used to declare structure variables of its type later in the program.

Declaring structure variables:

After defining a structures format we can declare variables of that type. A structure variable declaration is similar to the declaration of variables of away other data types. It includes the full elements.

1. The keyword strut

2. The structures tap name

3. List of variable names separated by commas.

4. A terminating semicolon.

Ex: Strut book_bank book1, book2, book3;

Each one of these variables has he members as specified by the template.
Declares book1, book2 and book3 as variables of type struct book_bank.

The complete declaration might look like this:

strcut book_bank
{
char title [20];
char author [15];
int pages;
float price;
};

struct book_bank book1, book2, book3;

Remember that the members of a structure themselves are not variables .They do not occupy away memory until they are associated with the structure variables declaration in one statement

struct book_bank
{
char title [20];
char author [15];
int pages;
float price,
}
book1, book2, book3;

Accessing Structure Members:

We can access and assign values to the members of a structure win a number of ways.

The members of a structure should be linked to the structure variables in order to make them meaningful members.

The link between a member and a variable is established using the member operator ‘.' which is also known as dot operator.

Ex: book1.price

is the variable representing the price of book1 and can be treated like any other ordinary variable.

strcpy ( book1.title, “BASIC”);

strcpy ( book1.author, “Balagusamy”);

book1.price = 120.50;

We can also use scan to give the values through the keyboard.

scanf (“%s\n”, book1.title);

scanf (“%d \n” ,& book1.pages);

Structure Initialization:

Like another data type a structure variable can be initialized at compile time.

main ( )
{
struct
{
int weight;
float height;
}
stu = { 60,180.75};
………………….
}

This assigns the value 60 to stu.weight and 180.75 stu.Height.

The following statements initialize 2 structure variables. Here, it is essential to use a tag name.

main ( )
{
struct st_record
{
int weight;
float height;
};
struct st_record stu 1= {60,180.75};
struct st_record stu 2= {53,170.60};
…………………….
}

Another method is to initialize a structure variable outside the function  as shown below.

struct st_record
{
int weight ;
float height;
}
stu = {60,180.75};
main ( )
{
struct st_record stu 2= {50,170.20};
…………….
}

The compile time initialization of a structure variables must have the following elements.

1. The keyword structure

2. The structure tag name

3. The name of the variable to be declared.

4. The assignment operator =

5. A set of values for the members of the structure variables, separated by commas and enclosed in braces.

6. A terminating semicolon.




Related

C Language 4238034642159220397

Post a Comment

emo-but-icon

item