Structures

Filter Course


Structures

Published by: Nuru

Published date: 21 Jun 2021

Structures Photo

Structures

Introduction

Arrays are used to store large sets of data and manipulate them but the disadvantage is that all the elements stored in an array are to be of the same data type. If we need to use a collection of different data type items it is not possible using an array. When we require using a collection of different data items of different data types we can use a structure. The structure is a method of grouping data of different types. Structures are a convenient method of handling a group of related data items of different data types.

Example of Structure

struct Student
{
    char name[25];
    int age;
    char branch[10];
    // F for female and M for male
    char gender;
};

Here struct Student declares a structure to hold the details of a student which consists of 4 data fields, namely nameagebranch and gender. These fields are called structure elements or members.

Each member can have different datatype, like in this case, name is an array of char type and age is of int type etc. The student is the name of the structure and is called the structure tag.

 

Declaring Structure Pointers

Assuming the previously defined structure addr, the following declares addr_pointer as a pointer to data of that type:
struct addr *addr_pointer;

 REFERENCING STRUCTURE ELEMENTS 

Individual elements of a structure are referenced using the dot operator. The pointer to a structure member operator -> is used to access a member of a structure using a pointer variable.

Accessing Structure members using the dot operator
Individual elements of a structure are referenced using the dot operator, for example:
addr_info. postalcode = 1234;
printf ("%d",addr_info. postalcode);
Example:
# include
# include
struct
{
char name[15]; /* childs name */
int age; /* childs age */
int grade; /* childs grade in school */
} boy, girl;
int main()
{
strcpy (boy.name, “Herbert”);
boy.age = 15;
boy.grade = 75;
girl.age = boy.age - 1; /* she is one year younger */
girl.grade = 82;
strcpy (girl.name, “Fousett”);
printf ("%s is %d years old and got a grade of %d\n",
girl.name, girl.age, girl.grade);
printf ("%s is %d years old and got a grade of %d\n",
boy.name, boy.age, boy.grade);
return 0;
}
Output:
Fausett is 14 years old and got a grade of 82
Herbert is 15 years old and got a grade of 75

Accessing Structure Pointers using -> operator
Structure pointers may be used to generate a call by reference to a function and to create linked
lists and other dynamic data structures. Call by reference can be used for structures to avoid
overheads occurred by the push and pop operations of all the structure elements of the stack. For
example if:
struct addr_info *ptr;
To access the elements of a structure using a pointer, the -> operator is used:

ptr -> postalcode
where ptr has been declared as a pointer to the type of structure and assigned the address of a
variable of that type and postalcode is a structural element within that variable.
This can also be expressed as:
(*ptr). postalcode
The parenthesis is necessary because the structure member operator “. “ takes higher precedence
than does the indirection operator.

 

Arrays of structure

It is possible to define an 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];

 

An array of structures can be assigned initial values just as any other array can. Remember that each element is a structure that must be assigned corresponding initial values as illustrated below.

#include< stdio.h >

void main()

{

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);

for(i=0;i < n;i++)

{

printf(“ Enter Id_no,name address combination age\m”);

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 structure student contains another structure date as its one of its members.

 

Functions and structures

We can pass structures as arguments to functions. Unlike array names, however, which always point to the start of the array, structure names are not pointers. As a result, when we change the structure parameter inside a function, we don‟t affect its corresponding argument.

 

Passing structure to elements to functions

A structure may be passed into a function as individual member or a separate variable. A program example to display the contents of a structure passing the individual elements to a function is shown below.

#include

#include

void fun(float,int);

void main()

{

struct student

{

float marks;

int id;

};

struct student s1={67.5,14};

fun(s1.marks,s1.id);

getch();

}

void fun(float marks,int id)

{

printf("\nMarks:%f",marks);

printf("\nID:%d",id);

}

It can be realized that to pass individual elements would become more tedious as the number of structure elements go on increasing a better way would be to pass the entire structure variable at a time.

Passing entire structure to functions

In case of structures having to have numerous structure elements passing these individual elements would be a tedious task. In such cases we may pass a whole structure to a function as shown below:

# include stdio.h>

{

int emp_id;

char name[25];

char department[10];

float salary;

};

void main()

{

static struct employee emp1= { 12, “sadanand”, “computer”, 7500.00 };

/*sending entire employee structure*/

display(emp1);

}

/*function to pass entire structure variable*/

display(empf)

struct employee empf

{

printf(“%d%s,%s,%f”, empf.empid,empf.name,empf.department,empf.salary);

}

Pointers to struct

Pointers can be used to refer to a struct by its address. This is useful for passing structs to a function. The pointer can be dereferenced using the * operator. The -> operator dereferences the pointer to struct (left operand) and then accesses the value of a member of the struct (right operand).

struct point {
   int x;
   int y;
};
struct point my_point = { 3, 7 };
struct point *p = &my_point;  /* p is a pointer to my_point */
(*p).x = 8;                   /* set the first member of the struct */
p->x = 8;                     /* equivalent method to set the first member of the struct */