Pointers in C programming are variables that hold the memory locations of other variables. They play a crucial role in managing data efficiently by enabling direct memory access and manipulation. Through pointers, developers can efficiently handle dynamic memory allocation, improve performance, and pass large data structures to functions without the need for duplication.
What is Array?
A group of elements with identical data types stored in contiguous memory locations is referred to as an array. Each element can be accessed using a zero-based index. Arrays play a crucial role in organizing data in C programming by enabling efficient storage and retrieval of structured information.
Algorithm:
The algorithm for determining the largest element in an array is a comparison-based approach. It begins by comparing the first two elements and setting the larger one as the current maximum. After that, it proceeds to iterate through the remaining elements in the array, comparing each with the current maximum and updating it if a larger element is found.
- Begin
- Define the necessary variables: num: The integer variable is used to store the number of elements in the array. ptr: Pointer to an integer, used for iterating over the array. max_num: The array's maximum element is stored in the integer variable.
- Request the user to enter the array's element array (num).
- The array's memory is dynamically allocated according to the value of num.
- Verify if the memory allocation is successful. Otherwise, terminate the program and provide an error message.
- Request that the user enter each array element individually.
- Iterate through the array elements using a loop: Set ptr to point to the array's first element. Consider the first element (*ptr) as the maximum (maxnum). Iterate through each of the array elements: Move the pointer to the next element. Compare the current element with maxnum. The value of max_num should be updated to reflect the current element if it exceeds it.
- Once the array has been iterated through, max_num will hold the largest element.
- Display the largest element that was found.
- Release the dynamically allocated Memory.
- End.
- num: The integer variable is used to store the number of elements in the array.
- ptr: Pointer to an integer, used for iterating over the array.
- max_num: The array's maximum element is stored in the integer variable.
- Set ptr to point to the array's first element.
- Consider the first element (*ptr) as the maximum (max_num).
- Iterate through each of the array elements:
- Move the pointer to the next element.
- Compare the current element with max_num.
- The value of max_num should be updated to reflect the current element if it exceeds it.
Example:
Let's consider an illustration to determine the maximum element in an array by utilizing a pointer in the C programming language.
#include <stdio.h>
#include<stdlib.h>
int main()
{
int num, *ptr, max_num;
printf("Please enter the number of elements in the array: ");
scanf("%d", &num);
//Memory for the array should be dynamically allocated.
int *a = (int *)malloc(num * sizeof(int));
if (a == NULL)
{
printf("Memory allocation is failed.\n");
return 1;
}
printf("Enter the %d integers one by one:\n", num);
for (int q = 0; q < num; q++)
{
scanf("%d", &a[q]);
}
// Pointing to the first element of the array.
ptr = a;
// Assume that the first element is the maximum.
max_num = *ptr;
// Find the maximum element by iterating over the array.
for (int i = 1; i < num; i++)
{
// Move to the next element using the pointer.
ptr++;
// Update max_num if the element currently in use is greater than max_num.
if (*ptr > max_num)
{
max_num = *ptr;
}
}
printf("The largest element in the array is: %d\n", max_num);
// Free Memory that is dynamically allocated.
free(a);
return 0;
}
Output:
Please enter the number of elements in the array: 7
Enter the 7 integers one by one:
37 26 39 68 45 19 51
The largest element in the array is: 68
Explanation:
- Include Headers:
The program incorporates the header files stdlib.h for memory allocation and deallocation functions like free and malloc and stdio.h for input/output operations.
- Declaration of Variables:
- num: The number of elements in the array is stored in this integer variable.
- ptr: An integer pointer that will be utilized to traverse across the array.
- max_num: An integer variable used to store the array's maximum element.
- Ask for for User Input:
The program utilizes the printf function to prompt the user for the number of elements in the array, while scanf function is employed to capture the user input.
- Dynamic Memory Allocation:
Memory is dynamically reserved for the array using the Malloc function. The allocated memory size is calculated by multiplying the integer count by num * sizeof(int).
- Confirming Memory Allocation:
The software validates to check if the memory allocation process was successful. In the scenario where malloc returns NULL, it indicates a failure in memory allocation, prompting the display of an error message. In such instances, the program halts with an error code (return 1;).
- Provide Input Array Elements:
Set a pointer to manage the dynamically allocated memory for the array "a" and establish the maximum value for storage.
The first element of the array corresponds to the pointer referenced by "ptr". The value of the initial array element is employed to set the max_num variable.
- Locate the Maximum Element:
Commencing from the second element (i = 1), the software loops through the array. It compares the existing maximum (maxnum) with each element. If an element is larger than maxnum, max_num is replaced with the value of that element.
- Display Maximum Element:
The software loops through the entire array and displays the highest element discovered.
- Release Dynamically Allocated Memory:
To avoid memory leaks, the dynamically allocated memory of the array is eventually freed using the free function.