Using structures C

  • Structure in structure.
  • Array of structures.
  • Pointer to structure.
  • Passing structure as parameter.

We have seen all the basics of structures. Now we will see how to use structures for the following.

  • how to use in relation to pointers and functions
  • how array of structures can be formed and used
  • how structures can be embedded in each other.

Structure in structure
Member of a structure can be a structure itself. When does this need arise? When a member of a structure has to be further broken down into entities that member can be declared as structure. Suppose we want to add address of the employee, in the employee records, address will consist of phone, city and pin. Hence a structure can be defined as

Thus member ‘a’ is itself a structure. In such cases declaration of embedded structure must appear before the declaration of the outer structure.

The struct emp can now be initialized as

struct emp e1 = (“John”, 5, 18844.70, “022-5334716”, “Mumbai”, 411421};

i.e. just append the member of embedded structure to the main structure.
The member of embedded structure can be obtained with an additional dot operator. If we want to obtain the city from employees address it is written as e1. a. city;

Array of structures
Since we found only arrays insufficient to solve real life problems, we studied structures. But, if we have a large number of employees say 100, we will need to define 100 structure variables. Arrays again come to our rescue. We can define array of structures just like we define array of variables of any other data-type.

The array of structures is defined and operation on this array is performed just like our usual array of other data types. When we define array of integers, we say

int num[10];

The general format for array definition is

Data-type array-name[size of array];

The definition below groups four characteristics of a rectangle into a single struct called rectangle.

What this definition does not do is get any memory allocated -- what it does do is create a new programmer-defined data type. Consequently, no data can be stored in rectangle. To get memory allocated, we need to declare a data structure of this type, such as is done below.

struct rectangle rect;

This structure consists of four members which can be accessed by means of the dot operator, which is also known as the structure member operator. The following example assigns the value 13.2 to the width member.

rect.w = 13.2;

Obviously, there needs to be a method for using a pointer to this structure. This is needed, for example, for calls to scanf() and in functions where you wish to change values stored in the structure. The scanf() call below uses the address-of operator to store data in the structure member for the width.

scanf("%f", &rect.w);

The following declares r to be a pointer to a structure of type rectangle and then assigns to it the address of the rect data structure.

Once the address of the structure has been assigned to the pointer variable, the arrow operator (also known as the structure pointer operator) is used to access individual members. It gets is name from the fact that -> looks like an arrow. Here the product of the length and width members is assigned to the area member.

Pointer to structure
Just as we have pointer to integer, pointer to array etc. We can also get pointer to a structure using the ‘&’ operator. Again taking the struct emp, we define a variable e1 of type struct emp

struct emp e1;

&e1 gives the starting address of the structure. The starting address of structure is depicted below.

Pointer to structure
If we have a variable

Data-type var

then
data-type *ptr_var gives pointer to the same type.
ptr_var = &var assigns address of ‘var’ to pointer variable.

Similarly

struct emp e1;

defines a variable e1 of struct emp type
struct emp *ptr_emp defines a pointer to struct emp type.
ptr_emp = &e1 makes ptr_emp to point to e1

Variable and pointer variable declaration can be combined together with structure format definition as

How do we access members of structure if we have a pointer to structure?

ptvar -> member

where ptvar is a structure type pointer variable and the operator -> is comparable to (.)period operator. ‘.’ operator requires a structure variable, while -> operator requires a structure pointer on its left side.

Let us see how we can access individual members of structure using the structure pointer. The following table gives how we can access different types of members of a structure. i.e. how we access the member if it is an ordinary variable, array or a structure itself.

ordinary variable, array or a structure itself.

Since ptr_emp is a pointer to structure, (*ptr_emp). Salary gives contents of member salary. The parenthesis are necessary because precedence of structure member operator ‘.’ Is higher than ‘*’. The expression *ptr_emp.emp_no is illegal as emp_no is not a pointer

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

C Topics