pointer structures
pointer structures
// Deallocate memory
free(ptr);
return 0;
Common Errors:
Definition of Structure:
A structure (also known as a struct) is a collection of variables of different data types that are stored
together in memory. It is a user-defined data type that allows you to combine multiple variables into
a single unit.
Creating a Structure:
To create a structure, you use the struct keyword followed by the name of the structure and the
variables that make up the structure.
struct Person {
int age;
char name[20];
float height;
};
In this example, we have created a structure called Person that has three variables: age, name, and
height.
To access the members of a structure, you use the dot (.) operator.
person1.age = 25;
strcpy(person1.name, "John");
person1.height = 175.5;
In this example, we are accessing the members of the person1 structure variable using the dot
operator.
In this example, we are using a pointer ptr to access the members of the person1 structure variable.
Array of Structures:
To access the members of an array of structures, you use the array index and the dot operator.
persons[0].age = 25;
strcpy(persons[0].name, "John");
persons[0].height = 175.5;
In this example, we are accessing the members of the first element of the persons array.
Array of Structures:
An array of structures is a collection of structures of the same type stored in contiguous memory
locations. Each element of the array is a structure, and each structure can have multiple members.
Example:
struct Student {
int roll_no;
char name[20];
float marks;
};
};
To access the members of an array of structures, you use the array index and the dot operator.
Example:
In this example, we are accessing the members of the first element of the students array.
1. Efficient memory usage: Array of structures allows for efficient memory usage, as multiple
structures are stored in contiguous memory locations.
2. Easy access: Members of an array of structures can be easily accessed using the array index and
dot operator.
3. Simplified code: Array of structures can simplify code, as you can perform operations on multiple
structures using a single loop.
Structure Pointer:
A structure pointer is a pointer variable that points to a structure. It is a variable that holds the
memory address of a structure.
Example:
struct Student {
int roll_no;
char name[20];
float marks;
};
In this example, we have declared a structure pointer ptr that points to a structure of type Student.
Example:
In this example, we have initialized the structure pointer ptr with the address of the structure
variable student.
To access the members of a structure using a structure pointer, you use the arrow operator (->).
Example:
You can dereference a structure pointer using the dereference operator (*).
Example:
(*ptr).roll_no = 2;
strcpy((*ptr).name, "Alice");
(*ptr).marks = 90.0;
In this example, we are dereferencing the structure pointer ptr and accessing its members using the
dot operator.
1. Efficient memory usage: Structure pointers allow for efficient memory usage, as you can pass
structures to functions without copying the entire structure.
2. Improved code readability: Structure pointers can improve code readability, as you can use the
arrow operator to access structure members.
3. Simplified code: Structure pointers can simplify code, as you can perform operations on structures
using a single pointer.
A union is a special data type in C that allows storing different types of data in the same memory
location. It is a collection of variables of different data types that share the same memory space.
Declaring a Union:
union union_name {
data_type member1;
data_type member2;
...
data_type memberN;
};
Example:
union Data {
int i;