Structure in Data Structure

In this section of the tutorial, we will discuss the concept of a custom data type, i.e. Structure and its uses in a Data StructureWhile operating a Data Structurewe can construct a custom data type in five ways.

Now let’s move further to the introduction of the first custom data type, i.e. Structures.

What is Structure in Data Structure?

Structure is a collection of variables of different data types referenced under a single name. It makes a structure that packs data items of different data types. It provides a convenient means of collecting a group of logically related information together.

  • Structure help create any complex data items in a much meaningful and organized way.
  • It is often used in creating a complex program that provides an analogous concept of maintaining a record.
  • When we declare a Structure, it forms a template that is used to create Structure objects (i.e. instances of a Structure).
  • The variables inside the Structure are called members(also called elements or fields).

Example:

struct addr
{
   char name[20];
   char street[50];
   char city[15];
   char state[10];
   unsigned long int zip;
};


Operations on Structure in Data Structure

Any member of a Structure can be identified using the member operator or dot operator ‘.’When a Structure member and Structure variable is linked using the member operator, it can be treated like any other variable, and hence any type of expressional and operational manipulation can be done on it.
Example:

if(student1.roll == 17)


student1.marks += 20.00;


float sum = student1.marks + student2.marks;


student1.marks * = 1.5;

Increment and decrement operators can also be used for manipulating numeric type members.
Example:

student1.marks ++;

++ student1.marks;

Important note: Member operator has higher precedence than any arithmetic and relational operators and hence no parentheses are required.

Copying Structure Variables

Variables under the same Structure type can be copied to each other in the same way that we do with an ordinary variable.

Example:

student1 = student2;

student 2 = student 1;

student 1 = student3;

  • Here, student1, student2 and student3 are the variables having a common Structure type.

Comparing Structure Variables

We can compare each member individually. We cannot compare two Structure variables as it is invalid to perform any logical operations on Structure variables.

Valid operations:

student1.marks == student2.marks;

student2.marks != student3.marks;

Invalid operations:

student1 == student2;

student2 != student3;

Arrays of Structures in Data Structure

We can declare an Array of Structures, where each element of the Array represents a Structure variable. This property helps in writing a program where we want to create a Structure having multiple variables for individual members of Structures. We will look into an example to understand it.

Example:

struct class student[80]
{
   int subject1;
   int subject2;
   int subject3;
};
main()
{
   struct score student[3] = { {65, 70, 80}, {75, 55, 85}, {90, 60, 50} };
}
  • Here, an Array named student is declared of three elements namely, student[0], student[1], and student[2].
  • The elements are initialized with their members.
  • The Array is declared in its standard form just like any other Array.


Arrays within Structures

We can use Arrays as Structure members. This means we can create one-dimensional or multi-dimensional Arrays of type integer or float inside a Structure type.

Example:

struct score
{
   int number;
   float subjects[3];
} student[3];
  • Here, subject is a Structure member which is also an Array that contains three elements i.e. subject[0], subject[1], and subject[2].
  • These elements can be accessed using standard Arrays accessing methods with member operators.

Structures and Functions in Data Structure

We can pass Structure values as arguments to Functions. There are multiple ways through which we can pass the Structure values from one Function to another.

We will now discuss the first method that involves passing a copy of the entire Structure to the called Function.

As we learned that a copy of the whole Structure is passed to the called Function, then the changes are only made to that copy of Structure. No changes are reflected in the original Structure in the calling function. After the changes are made, the called Function returns the entire Structure back to the calling function.

The general form of passing a copy of a Structure to a called Function is:

function_name (structure_variable_name);

The general form of the called Function having the Structure as the argument is:

data_type function_name (structure_variable_name)
{
   . . . . .
   . . . . .
   return (expression);
}

Important note:

  • The called Function must be declared with an appropriate type which must be relevant to the data type it is expected to return.
  • The Structure variable in the actual argument and the corresponding formal argument in the called Function must be of the same struct type.
  • We may only use a return statement when the called Function is expected to return some data value back to the calling Function.
  • When the called function returns a Structure, it must be assigned to an identical Structure type in the calling Function.

Structures and Pointers in Data Structure

Just like the name of an Array holds the address of its 0th element, likewise, the name of Arrays of Structure variables also holds the address of its 0th element. We will understand this with the help of an example given below.

Example:

struct collection
{
   char genre[20];
   int volumes;
   float price;
} book[2], *ptr;
  • Here, book is an Array of two elements of type struct collection.
  • And ptr is a Pointer to data objects of type struct collection.

ptr = book;

  • Now we assign the address of the 0th element of the book to ptr.
  • ptr will now point to the book[0].

Now we can access the Structure member by using the following notations:

ptr -> genre

ptr -> volumes

ptr -> price

or

(*ptr).genre

(*ptr).volumes

(*ptr).price

or

book[0].genre

book[0].volumes

book[0].price

  • Here, the symbol -> is called the arrow operator or member selection operator.

Now, we will look into an example to understand how we can use a Structure Pointer in a program.

Example: Program to store and print details of stocks

Code:

#include<stdio.h>
struct invent
{
    char *name[15];
    int number;
    float price;
};
int main()
{
    struct invent product[3],*ptr;
    ptr = &product[3];
    printf("Enter the values: \n");
    scanf("%s", ptr->name);
    scanf("%d", &ptr->number);
    scanf("%f", &ptr->price);
    printf("Your entered value:\n");
    printf("Item name: %s\n",ptr->name);
    printf("Quantity: %d\n",ptr->number);
    printf("Price per item: %f\n",ptr->price);
    return 0;
}

Output:

Enter the values: 
Shirts
70
1200
Your entered value:
Item name: Shirts
Quantity: 70
Price per item: 1200.000000

In the next section of the tutorial, we will discuss the use of Array in Data Structure and we will learn the properties of Array, the need of using Array, and important operations of Array that will be used thoroughly in this Data Structure tutorial.