Introduction
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.
Syntax:
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.
For example, if you want to define a structure named “Person” with members like name, age, and salary, the code would look like this:
struct Person {
char name[50];
int age;
float salary;
};
After defining the structure, you can declare variables of that structure type as follows:
//Declaration of structure variable
struct Person person1, person2;
Now, person1
and person2
are variables of type struct Person
. You can access the members of the structure using the dot operator (.) as follows:
//Calling or Accessing structure members
person1.age = 30;
person2.salary = 5000.00;
This sets the value of the age
member of person1
to 30 and the value of the salary
member of person2
to 5000.00.
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;
}