C- Structures

What is a Structure?

  • Structure is a method of packing the data of different types.
  • When we require using a collection of different data items of different data types in that situation we can use a structure.
  • A structure is used as a method of handling a group of related data items of different data types.

A structure is a collection of variables under a single name. These variables can be of different types, and each has a name which is used to select it from the structure. A structure is a convenient way of grouping several pieces of related information together.

A structure can be defined as a new named type, thus extending the number of available types. It can use other structures, arrays or pointers as some of its members, though this can get complicated unless you are careful.

 A structure is a customised user-defined data type in C. It is by definition a collection of variables of any type that are referenced under one name, providing a convenient means of keeping related information together.

Some terminology :-

structure definition  :-  the  template used to create structure variables.

structure elements  :-   the member variables of the structure type

Defining a Structure

A structure type is usually defined near to the start of a file using a typedef statement. typedef defines and names a new type, allowing its use throughout the program. typedefs usually occur just after the #define and #include statements in a file.

Here is an example structure definition.

   typedef struct {          char name[64];          char course[128];          int age;          int year;  } student;

Syntax :                       struct  tag   {

                                                type  var_1 ;

                                                type var_2 ;

                                                …

                                    type var_n ;

                                                }  ;

The keyword struct tells the compiler we are dealing with a structure and must be present whenever we refer to the new type, tag is an identifier which is the name given to the customised “type”.

A variable of this new type can now be defined as follows for example. Note that the keyword struct has to be used in conjunction with our own name for the structure, tag.

struct tag variable ;

For Example :-

struct RECORD {

int rec_no ;

char name[30] ;

char town[40] ;

char country[ 20 ]

} ;

struct RECORD person ;

Accessing Structure Elements

For example define a complex type structure as follows.

struct complex {

double real ;

double imaginary ;                   // Note that a variable may also be

} cplx ;                                     // defined at structure definition time

 

The elements of the structure are accessed using the dot operator, . , as follow

cplx.real  =  10.0 ;

cplx.imag  =  20.23 ;

scanf ( “%lf”, &cplx.real ) ;

or if we want to access struct RECORD already defined

puts( person.name ) ;

or character by character

person.name[i] = ‘a’ ;

 

Thus we treat structure elements exactly as normal variables and view the dot operator as just another appendage like the indirection operator or an array index

Initialising Structures

Structure elements or fields can be initialised to specific values as follows :-

struct id {

char name[20] ;

int id_no ;

} ;

struct id student = { “John”, 1212 } ;

 

Structure Pointers

 As we have said already we need call by reference calls which are much more efficient than normal call by value calls when passing structures as parameters. This applies even if we do not intend the function to change the structure argument.

A structure pointer is declared in the same way as any pointer for example

struct address {

char name[20] ;

char street[20] ;

} ;

struct address person ;

struct address *addr_ptr ;

declares a pointer addr_ptr to data type struct address.

To point to the variable person declared above we simply write

addr_ptr =  &person ;

which assigns the address of person to addr_ptr.

To access the elements using a pointer we need a new operator called the arrow operator, ->, which can be used only with structure pointers. For example

puts( addr_ptr -> name ) ;

 

Example :- Program using a structure to store time values.

#include <stdio.h>

struct time_var {

int hours, minutes, seconds ;

} ;

void display ( const struct time_var * ) ;                      /* note structure pointer and const */

void main()

{

struct time_var time ;

time.hours = 12 ;

time.minutes = 0 ;

time.seconds = 0 ;

display( &time ) ;

}

void display( const struct time_var *t )

{

printf( “%2d:%2d;%2d\n”, t -> hours, t -> minutes, t -> seconds ) ;

}

 

Arrays of structure:

It is possible to define a array of structures for example if we are maintaining information of all the students in the college and if 100 students are studying in the college. We need to use an array than single variables. We can define an array of structures as shown in the following example:

structure information
{
int id_no;
char name[20];
char address[20];
char combination[3];
int age;
}
student[100];

Example

#include< stdio.h >
{
struct info
{
int id_no;
char name[20];
char address[20];
char combination[3];
int age;
}
struct info std[100];
int I,n;
printf(“Enter the number of students”);
scanf(“%d”,&n);
printf(“ Enter Id_no,name address combination age\m”);
for(I=0;I < n;I++)
scanf(“%d%s%s%s%d”,&std[I].id_no,std[I].name,std[I].address,std[I].combination,&std[I].age);
printf(“\n Student information”);
for (I=0;I< n;I++)
printf(“%d%s%s%s%d\n”, ”,std[I].id_no,std[I].name,std[I].address,std[I].combination,std[I].age);
}

 

Structure within a structure:

A structure may be defined as a member of another structure. In such structures the declaration of the embedded structure must appear before the declarations of other structures.

struct date
{
int day;
int month;
int year;
};
struct student
{
int id_no;
char name[20];
char address[20];
char combination[3];
int age;
structure date def;
structure date doa;
}oldstudent, newstudent;

the sturucture student constains another structure date as its one of its members.

error: Content is protected !!