Structure and pointer
Structure and array
Function and array
November 27, 2024
2
Structures and Pointers
A structure in C is a user-defined data type that groups variables
of different data types under a single name. Pointers can be used
to manipulate structures, just like other variables.
Using Structure with Pointer
Pointers to structures allow you to dynamically access and
modify the structure members.
Simple Example
#include <stdio.h>
#include <string.h>
// Define a structure
struct Student {
int id;
char name[50];
3
float marks;
};
int main() {
// Declare a structure variable and a pointer to it
struct Student student1;
struct Student *ptr;
// Point to the structure
ptr = &student1;
// Access structure members using the pointer
ptr->id = 101; // Same as (*ptr).id = 101;
strcpy(ptr->name, "Ali"); // Copy string into name
ptr->marks = 95.5;
// Display structure members
4
printf("ID: %d\n", ptr->id);
printf("Name: %s\n", ptr->name);
printf("Marks: %.2f\n", ptr->marks);
return 0;
Explanation
Structure Definition:
The Student structure has three members: id, name, and marks.
Pointer Declaration:
struct Student *ptr is a pointer to the Student structure.
Pointer Assignment:
The pointer ptr is assigned the address of student1 using
&student1.
Access Members Using Pointer:
5
Use the -> operator to access structure members via the pointer.
Example: ptr->id is equivalent to (*ptr).id.
Output: The program outputs the details stored in the Student
structure:
ID: 101
Name: Ali
Marks: 95.50
Key Notes
The -> operator is shorthand for dereferencing a pointer and
accessing a structure member.
Structures and pointers together are powerful for dynamic
memory manipulation and passing structures to functions.
Structures and Arrays in C
6
In C, both structures and arrays are important concepts for
managing and storing data. A structure allows you to store
different types of data together, while an array stores multiple
elements.
A structure is used to store different types of data.
An array of structures allows you to store multiple structures in a
single variable.
Simple example that demonstrates how to use structures and
arrays together in C
Example: Array of Structures
This example demonstrates how to use an array of structures to
store details of multiple students.
#include <stdio.h>
#include <string.h>
// Define a structure for Student
struct Student {
7
int id;
char name[50];
float marks;
};
int main() {
// Declare an array of structures
struct Student students[3];
// Assign values to the first student
students[0].id = 101;
strcpy(students[0].name, "Ali");
students[0].marks = 85.5;
// Assign values to the second student
students[1].id = 102;
8
strcpy(students[1].name, "Sara");
students[1].marks = 90.0;
// Assign values to the third student
students[2].id = 103;
strcpy(students[2].name, "Ahmed");
students[2].marks = 88.5;
// Display the details of all students
for (int i = 0; i < 3; i++) {
printf("Student %d:\n", i + 1);
printf("ID: %d\n", students[i].id);
printf("Name: %s\n", students[i].name);
printf("Marks: %.2f\n\n", students[i].marks);
}
9
return 0;
Explanation:
Structure Definition:
struct Student is a structure that holds:
id (an integer representing the student ID)
name (a string for the student's name)
marks (a floating-point value for the student's marks)
Array of Structures:
struct Student students[3]; declares an array of 3 Student
structures.
This array will hold details for 3 students.
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:
A for loop is used to print the details of each student.
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:
Structures allow you to group different types of data together
(e.g., ID, name, marks).
Arrays of structures allow you to manage multiple instances of
the structure. In this example, an array holds data for 3 students.
Functions and Arrays
In C, functions allow you to modularize your code, and arrays
are used to store multiple values. You can pass arrays to
functions to process or modify their elements.
Simple Example of Function and Array
This example demonstrates passing an array to a function and
modifying its elements.
12
#include <stdio.h>
// Function to modify the array elements
void modifyArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
arr[i] = arr[i] * 2; // Double each element
int main() {
int arr[5] = {1, 2, 3, 4, 5};
// Display the original array
printf("Original Array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
13
printf("\n");
// Call the function to modify the array
modifyArray(arr, 5);
// Display the modified array
printf("Modified Array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
printf("\n");
14
return 0;
Explanation:
Function Definition:
modifyArray(int arr[], int size) is a function that accepts an array
(arr[]) and its size (size).
It loops through the array, doubling each element (arr[i] = arr[i]
* 2).
Main Function:
The arr[] is defined with values {1, 2, 3, 4, 5}.
The array is passed to modifyArray(arr, 5) for modification.
Displaying Arrays:
The original array is printed.
The modified array (where each element has been doubled) is
printed.
15
Output:
Original Array: 1 2 3 4 5
Modified Array: 2 4 6 8 10