Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Structure

Structure

A structure (struct) is a user-defined data type that allows you to group different types of variables together under a single name. It is useful when you want to store related data of different types. For better understanding see the short https://www.youtube.com/shorts/shRtvmEMfWE

The syntax of defining a structure in C is as follows:

//Definition of structure
struct structure_name {
   data_type member1;
   data_type member2;
   //more members...
};

Here, struct is the keyword used to define a structure. structure_name is the name of the structure, and it can be any valid C identifier. data_type specifies the data type of the members, and member1, member2, etc. are the names of the members of the structure.

Lets take a refrigerator example to understand structure better.

A refrigerator is a household appliance that is used to store food and beverages at a low temperature to prevent spoilage. Just like a structure in C programming, a refrigerator is designed to hold different types of items and keep them organized.

In the case of a refrigerator, we can think of it as having different compartments for storing different types of items. For example, there might be a compartment for storing fruits and vegetables, another for storing dairy products, and yet another for storing beverages.

In C programming, we can create a structure that represents a refrigerator, and define different variables within the structure to represent the different compartments. For example, we might define a “fridge” structure that includes variables for the “fruit_veg_compartment”, “dairy_compartment”, and “beverage_compartment”.

We can then use this structure to store and organize information about the items stored in the refrigerator. For example, we might define a variable of type “fridge” called “my_fridge”, and set the values of the “fruit_veg_compartment” and “dairy_compartment” variables to represent the items stored in those compartments.

By using a structure to represent a refrigerator, we can write more modular and maintainable code that is easier to understand and update. We can also easily access and manipulate the different variables within the structure, just like we can easily access and organize the different compartments in a real refrigerator.

Example:

#include <stdio.h>

// Define the fridge structure with different compartments
struct Fridge {
    int fruit_veg_compartment;  // Number of items in the fruit and vegetable compartment
    int dairy_compartment;      // Number of items in the dairy compartment
    int beverage_compartment;   // Number of beverages in the compartment
};

int main() {
    // Declare a variable of type 'Fridge' called 'my_fridge'
    struct Fridge my_fridge;

    // Set values for each compartment
    my_fridge.fruit_veg_compartment = 10;  // e.g., 10 fruits and vegetables
    my_fridge.dairy_compartment = 5;       // e.g., 5 dairy products
    my_fridge.beverage_compartment = 8;    // e.g., 8 beverages

    // Print the contents of each compartment
    printf("Items in the fruit and vegetable compartment: %d\n", my_fridge.fruit_veg_compartment);
    printf("Items in the dairy compartment: %d\n", my_fridge.dairy_compartment);
    printf("Items in the beverage compartment: %d\n", my_fridge.beverage_compartment);

    return 0;
}

Output:

Items in the fruit and vegetable compartment: 10
Items in the dairy compartment: 5
Items in the beverage compartment: 8

Explanation:

  1. Structure Definition:
struct Fridge {
    int fruit_veg_compartment;
    int dairy_compartment;
    int beverage_compartment;
};
  • The structure Fridge has three members representing different compartments of the refrigerator.
  • Each member stores the number of items in that specific compartment.

2. Variable Declaration:

struct Fridge my_fridge;
  • We create a variable my_fridge of type struct Fridge to hold the compartment data for one refrigerator.

3. Assigning Values:

my_fridge.fruit_veg_compartment = 10;
my_fridge.dairy_compartment = 5;
my_fridge.beverage_compartment = 8;
  • We assign values to the different compartments, representing the number of items in each.

4. Printing Values:

printf("Items in the fruit and vegetable compartment: %d\n", my_fridge.fruit_veg_compartment);
printf("Items in the dairy compartment: %d\n", my_fridge.dairy_compartment);
printf("Items in the beverage compartment: %d\n", my_fridge.beverage_compartment);
  • We use printf to display the number of items in each compartment.

Advance Example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Define the person structure
struct person {
    char* name;
    int age;
    char* address;
};

// Function to create a new person object
struct person* create_person(char* name, int age, char* address) {
    // Allocate memory for the person object
    struct person* p = malloc(sizeof(struct person));

    // Allocate memory for the name and address strings
    p->name = malloc(strlen(name) + 1);
    p->address = malloc(strlen(address) + 1);

    // Copy the name and address strings to the new memory locations
    strcpy(p->name, name);
    p->age = age;
    strcpy(p->address, address);

    return p;
}

// Function to print out a person's information
void print_person(struct person* p) {
    printf("Name: %s\n", p->name);
    printf("Age: %d\n", p->age);
    printf("Address: %s\n", p->address);
}

int main() {
    // Create a new person object
    struct person* p = create_person("John Smith", 30, "123 Main Street, Anytown USA");

    // Print out the person's information
    print_person(p);

    // Free the memory used by the person object
    free(p->name);
    free(p->address);
    free(p);

    return 0;
}

    Leave a Reply

    Your email address will not be published.

    Need Help?