Back to C Programming
2026-07-155 min read

Find the largest number (Dynamic memory allocation is used)

Learn Find the largest number (Dynamic memory allocation is used) step by step with clear examples and exercises.

Why This Matters

Welcome to this full guide on finding the largest number using dynamic memory allocation in C programming! We will walk you through the core concept, a worked example, common mistakes, practice questions, and frequently asked questions. By the end of this lesson, you'll be able to write your own code to find the largest number in an array using dynamic memory allocation.

Why This Matters

In real-world programming scenarios, you often encounter situations where the size of the input data is not known beforehand. In such cases, dynamic memory allocation comes in handy as it allows you to allocate memory during runtime based on the actual data size. Finding the largest number in an array using dynamic memory allocation is a fundamental problem that helps you understand and master this essential C programming concept.

Prerequisites

To follow along with this lesson, you should have a good understanding of the following topics:

  • C Pointers
  • Dynamic Memory Allocation (malloc(), calloc(), free())
  • Basic C Programming Concepts (variables, data types, operators)

Core Concept

To find the largest number in an array using dynamic memory allocation, follow these steps:

  1. Allocate memory for the array using malloc().
  2. Read input numbers and store them in the allocated memory.
  3. Initialize a variable to hold the largest number found so far.
  4. Iterate through the array and compare each number with the current largest number. If a larger number is found, update the largest number variable accordingly.
  5. After iterating through the entire array, print out the largest number.
  6. Don't forget to free the allocated memory using free() when you're done.

Here's an example of how this can be implemented:

#include <stdio.h>
#include <stdlib.h>

int main() {
int *arr; // Declare a pointer to an integer array
int n, i, largest = INT_MIN; // Initialize variables

printf("Enter the total number of elements: ");
scanf("%d", &n);

// Allocate memory for n integers
arr = (int *)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Error!!! Memory not allocated.");
return 1;
}

printf("Enter %d numbers:\n", n);
// Read and store input numbers in the array
for (i = 0; i < n; ++i) {
scanf("%d", arr + i);
if (arr[i] > largest) {
largest = arr[i]; // Update largest number if a larger one is found
}
}

printf("Largest number = %d\n", largest);

free(arr); // Free the allocated memory when done
return 0;
}

Worked Example

Let's walk through a worked example to better understand how this code works:

  1. We first declare a pointer arr that will hold our integer array and initialize two variables, n for the total number of elements and largest with the minimum possible integer value (INT_MIN).
  1. We prompt the user to enter the total number of elements and read the input using scanf().
  1. Next, we allocate memory for n integers using malloc() and check if the allocation was successful. If not, we print an error message and exit the program.
  1. We then prompt the user to enter the numbers one by one and store them in the allocated memory using pointer arithmetic (arr + i). At the same time, we compare each number with the current largest number and update largest if a larger number is found.
  1. After reading all numbers, we print out the largest number and free the allocated memory using free().

Common Mistakes

Here are some common mistakes to avoid when implementing this code:

  1. Forgetting to check for memory allocation errors: Always check if memory is allocated successfully before using it, as shown in our example.
  2. Not initializing the largest number variable: Initialize largest with a suitable default value (e.g., INT_MIN) to ensure that the first number read is always compared against something.
  3. Misunderstanding pointer arithmetic: Remember that when using pointers, incrementing by 1 moves to the next memory location of the same data type as the pointed-to variable.
  4. Not freeing allocated memory: Always free the memory you've allocated using malloc() or calloc() once you're done with it to avoid memory leaks.
  5. Using an uninitialized pointer: Make sure to initialize your pointers before using them, either by allocating memory or assigning a valid address.

Practice Questions

  1. Modify the code to find the second-largest number in the array instead of the largest.
  2. Write a function that finds the smallest number in an array using dynamic memory allocation.
  3. Implement the same logic for finding the largest and smallest numbers in a 2D array (matrix).
  4. Modify the code to handle negative numbers and find the largest absolute number in the array.
  5. Write a program that finds all unique numbers in an array using dynamic memory allocation and stores them in another array.

FAQ

  1. Why do we need dynamic memory allocation for this problem? Dynamic memory allocation allows us to handle arrays of varying sizes at runtime, making our code more flexible and adaptable to different input data sizes.
  2. What happens if we don't free the allocated memory in our example? If we don't free the allocated memory, it will be leaked and cannot be reused by the program or operating system. This can lead to performance issues over time.
  3. Can we use calloc() instead of malloc() for this problem? Yes, you can use calloc() to initialize the memory with zeros before storing the input numbers. However, in our example, we chose to use malloc() since it only allocates memory without initializing it.
  4. What if the user enters more or fewer elements than specified? In such cases, you can modify the code to handle these scenarios appropriately, for example, by reallocating memory or prompting the user to enter the correct number of elements.
  5. Is it safe to cast the return value of malloc() to a pointer of the desired data type? Yes, it is safe to cast the return value of malloc() to a pointer of the desired data type as long as you check for memory allocation errors and free the allocated memory when done.