Structures in C Programming Language



The Structure is a special type of C data type. A structure contains a number of data types grouped together. Structure in C is one of the excellent functionality provided in C. Structure makes C Programming language easy and simpler up to certain extent. Structure is most widely used in Programming.

Structure in C allows multiple data types to be grouped together. As a programmer I have used structures in C a lot and find this feature interesting too. Just go through this article and you too will find structures in C interesting.

Let us suppose you want to store basic information about animal such as age, name, gender and birth place. There are two approaches that can be followed.
1)    Use one array each to store properties of animal as shown below.
int age[10];
char name[10][20] ;
char gender[10];
char bPlace[10][20];

This approach is very tedious as number of characteristics related to animal might increase. Also, we need to keep the track of index of each and every variable. Hence, it becomes very difficult to handle such C programs.

2)    Second approach is using structures in C.
At the end of this article you will understand the ease of using structures and its importance.
Declaring a Structure in C:
Structure in C is declared using the keyword ‘struct’.

Syntax:
struct <structure name>
{
    Element 1;
    Element 2;
    Element 3;
    .
    .
    .
    Element n;   
};
Please don’t forget to include semicolon (;) at the end of structure declaration as shown above.

For example, we can declare structure as follows:
struct animal
{
    int age;
    char *name;
    char gender;
    char *bPlace;
};

Here,
animal is the name given to structure.
age, name, gender and bPlace are the elements of the structure animal. We can call it as properties of the structure animal.

Declaring Structure variables:
Once the new structure data type has been defined, we can declare one or more variables of that type as shown below:

struct animal a1, a2;

This statement set aside space in memory. 

Following are the ways in which we can declare structure variables.
1)   
struct animal
{
    int age;
    char *name;
    char gender;
    char *bPlace;
}a1,a2;

2)
struct animal
{
    int age;
    char *name;
    char gender;
    char *bPlace;
};
struct animal a1, a2;

3)
struct animal
{
    int age;
    char *name;
    char gender;
    char *bPlace;
};
struct animal a1={12,”Sam”,’M’,”North America”};
struct animal a2={18,”Maxie”,’M’,”Amsterdam”};

Accessing structure Elements:
Now is the time to access structure elements after declaration of structure type and structure variables.

Syntax:
    <structure variable>.<structure element>

Consider,
struct animal
{
    int age;
    char *name ;
    char gender;
    char *bPlace;
}a1,a2;

Now, element ‘age’ for the variable ‘a1’ can be accessed using the statement,
a1.age;
Similarly, element ‘name’ for the variable ‘a1’ can be accessed using the statement,
a1.name;

Array of Structures:
We can create array of structure variables as follows:
struct animal
{
    int age;
    char *name ;
    char gender;
    char *bPlace;
}a[100];
   
The above declaration can store information of not more than 100 animals.
The values can be accessed as follows:
a[0].age =10;
The above statement will assign value 10 to the element ‘age’ of structure variable a[0].
Similarly,
We can assign values to the elements of other structure variables too.
a[1].age=12;

We can print the values as follows:
printf(“%d”,a[1].age);

The above statement will display 12.

Also See:


Responses

0 Respones to "Structures in C Programming Language"

Post a Comment

 

Total Pageviews

Return to top of page Copyright © 2011 | Kuppam Engineering College Converted into Blogger Template by Mohan Murthy