COMPLEX STRUCTURE DEFINITIONS


Introduction

This is out second tutorial based on Structures. I hope you have gone through the first one where we discussed all the basic of structures. In case if you have landed directly here then I request you to click here, to start from first tutorials.

C_Banner

So let’s start with advance structures in C, there are plenty of times in coding you end up using them. Few of the places are as follow:

  • Memory manipulation in C
  • arrays or arrays of structures
  • pointers to structures
  • Dynamic memory allocation
  • array of structure pointers
  • pointers to structures

pointers to structures Now we are going to discuss advance structures, first thing first where we can use these type of strictness we know, we can define structures of arrays or arrays of structures, pointers to structures, array of structure pointers, etc.

 

The following section gives definitions of complex structures.

 

struct adr_info      // A

{

char plot[30], street[30], city[30];

};

struct student_info         // B

{

char name[30];

float marks;

struct adr_info adr;     // C

};

main ( )

{

struct student_info student1; // D

struct student_info class[20];      // E

clrscr();

class[1].marks=90;     // F

strcpy(class[1].name,"Pardhu");

strcpy(class[1].adr.plot,"1/223");        // G

strcpy(class[1].adr.street," Kannelur Road");

strcpy(class[1].adr.city,"Jammalamadugu");

printf( " Marks are %f\n", class[1].marks);

printf( " name are %s\n", class[1].name);

printf( " adr.plot is %s\n", class[1].adr.plot);

printf( " adr.street is %s\n", class[1].adr.street);

printf( " adr.city is %s\n", class[1].adr.city);

getch();

}

Explanation

  • Statement A declares the adr_info of a structure containing the members plot, street and city.
  • Statement B declares a structure student_info having 3 members: name, marks, and adr. The data type of adr is structure adr_info, which is in by statement C.
  • Statement D defines the variable student1 of the data type struct student.
  • Statement E defines an array class with 20 elements. Each element is a structure.
  • You can refer to marks of the students of class[1] using the notation class[1].marks. class[1] indicates the first element of the array, and since each element is a structure, a member can be accessed using dot notation.
  • You can refer to the plot of a student of class[1] using the notation class[1].adr.plot. Since the third element of the structure is adr, and plot is a member of adr, you can refer to members of the nested structures.
  • If you want to refer to the first character of the character array plot, then you can refer it as member of nested structures
  • Class[1].adr.plot[0] because plot is a character array.

Points to Remember

  • When a structure is a member of another structure it is called a nested structure.
  • You can define structures of arrays or arrays of structures, and the members are referred to using dot notations.

 

Pointer to a structure:

 Like to every data type availabe in C, structure can also be pointed by a pointer. This is we are going to see how we will declare a pointer variable to structure and how to access it.

Consider the situation like we need to create a data base for a company with the details of employee. Like that, we will be having so many employees in that company. So, in this situation we define a structure which contains the details of an employee.

 

struct emp       // A

{

char first_name[10];

char second_name[10];

int age;

int id;

float salary;

};

struct emp my_emp;    // B

void show_emp_details(struct emp* p);         // C

main()

{

struct emp* e1;            // D

clrscr();

e1 = &my_emp;          // E

strcpy(my_emp.first_name,"Partha");

strcpy(my_emp.second_name,"Saradhi");

my_emp.age=23;

my_emp.id=1001;

show_emp_details(e1);           // F

getch();

}

void show_emp_details(struct emp *p)          // G

{

printf("%s  ",p->first_name);  // H

printf("%s\n",p->second_name);

printf("%d\n",p->age);

printf("%d\n",p->id);              // I

printf("%d\n",(*p).id);            // J

}

Explanation:

  • statement A defines the structure emp which includes the employee details like first_name, second_name, age, id.
  • Statement B declares a structure variable my_emp which is a global one.
  • Statement C is the declaration of function which receives pointer variable to a structure and returns nothing.
  • Statement D declares a pointer variable to our emp structure.
  • Statement E stores the my_emp address which is a emp structure variable.
  • After that we updated the employee details.
  • Statement F invoke(calls) the function show_emp_details by passing e1 variable which is a pointer to our structure and for your information, it contains my_emp variable’s address.
  • Statement G show_emp_details function receives the address of a emp structure.
  • Statement H shows how to access the structure elements of emp by using structure pointer.
  • If you observe statement I and J, both are correct ways to access that structure element. Statement I is introduced in C instead of using statement J method of accessing the element.
  • Like this we can declare an array of pointer variables to store more number of employees or dynamically we can create every time when we need to store the employee details.

One sincere request to all the readers, please comment down and correct us, this site is trying to help people to understand embedded technology, this is only possible with your comments and corrections. Please subscribe to the newsletter to get latest project/article updates right in your email inbox. Take few moments and like zembedded facebook page, follow on tweeter, thanks!

Related posts:

About author

This article was written by admin

Admin has over twenty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles. Working presently as Development Manager in India. A firm Believer in Knowledge grows when it shared.

Comments

Comments (1)
  1. nethra says - Posted: September 30, 2013

    Thanks for this tutorial it helped me a lot in my academics……….

Leave your comment

Your email address will not be published. Required fields are marked *