Structure - Declaration, Initialization and Usage

 What is the need for structures?

C has built in primitive and derived data types and still not all real world problems can be solved using those types. We need a custom data type for different situations. For example, if you need to store 100 student record that consist of name, age and mobile number. To code that you will create 3 array variables each of size 100 i.e. name[100], age[100], mobile[100]. For three fields in student record it say seem feasible . But, think how cumbersome it would be to manage student record with more than 10 fields, in separate variables for single student. To overcome this we need a user defined data type, like structures in C programming language  to deal with these situations.

Structure Definition:

Structure is a user defined data type. It is a collection of different data type, to create a new data type.

For example, You can define your custom type for storing student record containing name, age and mobile. Creating structure type will allow you to handle all properties of student with single variable, instead of handling it separately.

In short it is a data structure in which we can collect different types of data into one entity.

 Declaration of Structure:

To declare or define a structure, we use struct keyword. It is a reserved word in the C . We must use it  for structure definition or its variable declaration.The following syntax is for defining a structure with its members.

Syntax:  

struct tagname      // where struct is the keyword and tagname is the structure name

{

           type member name 1;
           type member name 2;

           type member name 3;                                //Declaration of Structure members

            "           "

             "           "

             type member name n;

};

The definition must start with the keyword struct followed by the name of the structure called the tag name.

 The pieces of data that make up the structure are called structure members and they are placed in a group between opening and closing braces.  The structure members are declared using the same syntax as used for variables.  Semicolons are placed at the end of each structure member. List of structure members is called template next the structure variables has to be declared

Declaration of structure variables:

A data type is useless without variables. A data type defines various properties about data stored in memory. To use any type we must declare its variable. Hence, let us learn how to create our custom structure type objects also known as structure variable.

In C programming, there are two ways to declare a structure variable:

  1. Along with structure definition
  2. After structure definition

Declaration along with the structure definition

In this we can declare a structure variable along with structure before terminating the structure definition.

Syntax:

struct structure_name
 {
        member1_declaration;
        member2_declaration;
         ... ...
        memberN_declaration;
 }structure_variable;

So, if it is needed to declare student type variable along with student structure definition we can use this approach.

Example:

struct student

{

    char name[40];        // Student name

    int  age;             // Student age

    unsigned long mobile; // Student mobile number

}s1;

Declaration after structure definition

The other way to declare structure variable anywhere in program based on the structure scope. If structure is defined in global scope, we can declare its variable in main() function, any other functions and in the global section too.

Syntax:

struct tagname variablename

 where  struct is the Keyword ,tagname is the structure name and variablename is the   structure variable

Example:

struct student 

       char name[40];                      //Student name

       int age;                                    // Student name int age; 

       unsigned long mobile;           // Student mobile number

}; 

 struct student s1;                 // Declare student variable s1

 Accessing Structure fields:

Since structure is a complex data type, we cannot assign any value directly to it using assignment operator. We must assign data to individual structure members separately and C supports two operators to access structure members, using a structure variable.

  • Dot/period operator .

  • Arrow operator ->

Dot/period operator also known as member access operator. We use dot operator to access members of simple structure variable.

To refer to a member,  use the syntax ,

structurevariablename.member name;

‘.’ is the dot operator used between the structure variable  and structure member.

Example:

student1.age = 26;

Since structure is a user defined type and we can have pointers to any type. Hence, we may also create pointers to structure.and in that case  We use arrow operator -> to access structure member from pointer to structure.

Syntax:

pointer_to_structure->member_name;

Example:

// sptr2 is a pointer to student type sptr2->age = 29;

//Example to demonstrate the declaration and access the variable with its members.

#include <stdio.h>

#include <conio.h>

void main ()

{

struct student

{

       int id ; //Definition of structure

       char name [10]

 };

 struct student s1;                                   //declaration of variable s1 of structure type

printf (“Enter student id\n”)

scanf (“%d”, &s1.id);

printf(“Enter student name\n” ;

scanf (“%s, s1.name) ;

printf (“name = %s\t id = %d\t\n”, s1.name, s1.id);

}

Output:

Enter student id

123

Enter student name

Liah

name=Liah      id=123


Note:

Structures can be initialized following the structure declaration with a list of values for all its fields  

Example:  

                      struct student newstudent

                {

                        12345;

                        Samuel;

                  };

 

Here id of newstudent structure variable is assigned value 12345 and name takes the value Samuel.

Comments

Popular posts from this blog

DBMS - Program 6 - Insurance Database

Types of Addressing modes

Decision Tree Classification Algorithm