aiml notes
aiml notes
A structure in C is a user-defined data type that allows grouping related variables (of different types)
under one name. Structures are used to represent a record or an object by combining variables
(called members) into a single unit. This is especially useful when you need to handle complex data
like student records, employee details, or points in a coordinate system
struct StructureName {
DataType member1;
DataType member2;
};
Key Points:
3. Members: Variables inside the structure can have different data types.
1. Declaration of a Structure
When defining a structure, you specify its layout but do not allocate memory. For example:
struct Student {
};
2. Initialization of a Structure
Direct Initialization:
student2.id = 102;
student2.marks = 88.0;
3. Accessing Structure Members
Use the dot operator (.) when working with a structure variable.
Use the arrow operator (->) when working with a pointer to a structure.
#include <stdio.h>
// Define a structure
struct Student {
};
int main() {
printf("Student Details:\n");
student2.marks = 88.0;
return 0;
1. Structure Definition:
o The Student structure is defined with three members: id (integer), name (character
array), and marks (floating-point number).
2. Initialization:
o student2 is initialized using individual assignments (including strcpy for the string).
3. Accessing Members:
o The dot operator (.) is used to access and modify members of the structure variables
(student1 and student2).
4. Output:
o The program prints the details of both students, including the updated marks for
student1.
Output
Student Details:
ID: 101
Marks: 95.50
ID: 102
Marks: 88.00
Structure assignment allows assigning one structure variable to another of the same type. Unlike
arrays, structures in C support direct assignment of one variable to another, provided they belong to
the same structure type.
Syntax
var2 = var1;
This assignment copies the values of all members from var1 to var2.
Example
#include <stdio.h>
// Define a structure
struct Point {
};
int main() {
// Assign p1 to p2
p2 = p1;
p2.x = 30;
return 0;
Explanation
1. Structure Declaration:
2. Initialization:
3. Structure Assignment:
4. Verification:
o Modifying p2 does not affect p1, demonstrating that a new copy is created.
Output
Key Points
1. Deep Copy:
o Structure assignment creates a deep copy, meaning each member is copied from the
source to the destination.
o The structure cannot contain members that are arrays with incomplete types (e.g.,
flexible array members).
Array of Structures
Syntax
struct StructureName {
DataType member1;
DataType member2;
// More members...
};
#include <stdio.h>
struct Student {
int id;
char name[50];
float marks;
};
int main() {
printf("ID: ");
scanf("%d", &students[i].id);
printf("Name: ");
scanf("%s", students[i].name);
printf("Marks: ");
scanf("%f", &students[i].marks);
printf("\nStudent Details:\n");
return 0;
Output
ID: 101
Name: John
Marks: 95.5
Enter details for student 2:
ID: 102
Name: Jane
Marks: 88.0
ID: 103
Name: Mike
Marks: 76.5
Student Details:
2. Self-Referential Structures
A self-referential structure is a structure that contains a pointer to itself. These structures are
commonly used to create linked lists, trees, and other dynamic data structures.
Syntax
struct StructureName {
DataType member1;
};
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
int main() {
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
temp = temp->next;
}
printf("NULL\n");
free(head);
free(second);
free(third);
return 0;
Output
Key Points
Array of Structures:
Useful for grouping similar records together (e.g., multiple students, employees).
Self-Referential Structures:
Used to create dynamic data structures like linked lists, trees, etc.
Memory management (e.g., malloc and free) is crucial when dealing with dynamic
structures.
Dynamic Memory Allocation is the process of allocating memory during program runtime rather than
at compile time. This is particularly useful when the size or number of variables is not known in
advance. In C, memory is allocated dynamically using functions from the <stdlib.h> library, and
pointers are used to manage dynamically allocated memory.
o Syntax:
o Allocates memory for an array of elements and initializes all bytes to zero.
o Syntax:
3. realloc (Reallocation):
o Syntax:
4. free:
o Syntax:
#include <stdio.h>
#include <stdlib.h>
// Define a structure
struct Student {
int id;
char name[50];
float marks;
};
int main() {
if (studentPtr == NULL) {
return 1;
// Input details
printf("ID: ");
scanf("%d", &studentPtr->id);
printf("Name: ");
scanf("%s", studentPtr->name);
printf("Marks: ");
scanf("%f", &studentPtr->marks);
// Display details
printf("\nStudent Details:\n");
free(studentPtr);
return 0;
Explanation
ID: 101
Name: John
Marks: 95.5
Student Details:
ID: 101
Name: John
Marks: 95.50
#include <stdio.h>
#include <stdlib.h>
// Define a structure
struct Student {
int id;
char name[50];
float marks;
};
int main() {
int n;
scanf("%d", &n);
// Allocate memory for an array of structures
if (students == NULL) {
return 1;
printf("ID: ");
scanf("%d", &students[i].id);
printf("Name: ");
scanf("%s", students[i].name);
printf("Marks: ");
scanf("%f", &students[i].marks);
printf("\nStudent Details:\n");
free(students);
return 0;
}
Explanation
Output
ID: 101
Name: John
Marks: 95.5
ID: 102
Name: Jane
Marks: 88.0
Student Details:
Key Points
o Saves memory when the number of structure variables is unknown during compile
time.
2. Memory Management:
Dynamic memory allocation provides flexibility in memory usage, making it essential for efficient
resource management in C programs.