Structure and Pointer
Structure and Pointer
Simple Example
#include <stdio.h>
#include <string.h>
// Define a structure
struct Student {
int id;
char name[50];
3
float marks;
};
int main() {
ptr = &student1;
ptr->marks = 95.5;
return 0;
Explanation
Structure Definition:
The Student structure has three members: id, name, and marks.
Pointer Declaration:
Pointer Assignment:
Use the -> operator to access structure members via the pointer.
ID: 101
Name: Ali
Marks: 95.50
Key Notes
#include <stdio.h>
#include <string.h>
struct Student {
7
int id;
char name[50];
float marks;
};
int main() {
students[0].id = 101;
strcpy(students[0].name, "Ali");
students[0].marks = 85.5;
students[1].id = 102;
8
strcpy(students[1].name, "Sara");
students[1].marks = 90.0;
students[2].id = 103;
strcpy(students[2].name, "Ahmed");
students[2].marks = 88.5;
}
9
return 0;
Explanation:
Structure Definition:
Array of Structures:
Assigning Values:
10
The values of the student ID, name, and marks are assigned to
each structure in the array using the dot operator (.).
Displaying Data:
Output:
Student 1:
ID: 101
Name: Ali
Marks: 85.50
Student 2:
ID: 102
Name: Sara
Marks: 90.00
11
Student 3:
ID: 103
Name: Ahmed
Marks: 88.50
Summary:
#include <stdio.h>
int main() {
printf("\n");
modifyArray(arr, 5);
printf("\n");
14
return 0;
Explanation:
Function Definition:
Main Function:
Displaying Arrays:
Output:
Original Array: 1 2 3 4 5
Modified Array: 2 4 6 8 10