C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Pointers in C

Dynamic array with pointers

C Program: Dynamic array with pointers

Method 1: Using malloc() function

C

#include <stdio.h>

#include <stdlib.h>

 

int main() {

    int *arr;

    int n, i;

 

    // Input array size

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // Dynamically allocate memory for n integers

    arr = (int *)malloc(n * sizeof(int));

 

    // Check if memory allocation was successful

    if (arr == NULL) {

        printf("Memory allocation failed!\n");

        return 1;

    }

 

    // Input elements

    printf("Enter %d elements:\n", n);

    for (i = 0; i < n; i++) {

        scanf("%d", arr + i);

    }

 

    // Display elements

    printf("\nArray elements are:\n");

    for (i = 0; i < n; i++) {

        printf("%d ", *(arr + i));

    }

 

    // Free allocated memory

    free(arr);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter number of elements: 4
Enter 4 elements:
5 10 15 20

Array elements are:
5 10 15 20

Explanation

  • malloc() dynamically allocates memory at runtime.
  • The pointer arr holds the base address of the allocated memory.
  • Memory is accessed using pointer arithmetic (*(arr + i)).
  • free(arr); releases the allocated memory to prevent memory leaks.

 

C Program: Dynamic array with pointers

Method 2: Using calloc() function

C

#include <stdio.h>

#include <stdlib.h>

 

int main() {

    int *arr;          // pointer to hold base address of the array

    int n, i;

 

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // allocate memory using calloc()

    arr = (int *)calloc(n, sizeof(int));

 

    // check if memory allocation is successful

    if (arr == NULL) {

        printf("Memory not allocated.\n");

        return 1;

    }

 

    // input elements

    printf("Enter %d elements:\n", n);

    for (i = 0; i < n; i++) {

        scanf("%d", &arr[i]);

    }

 

    // display elements

    printf("Array elements are:\n");

    for (i = 0; i < n; i++) {

        printf("%d ", arr[i]);

    }

 

    // free allocated memory

    free(arr);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter number of elements: 4
Enter 4 elements:
5 10 15 20

Array elements are:
5 10 15 20

Explanation

  • calloc(n, sizeof(int)) dynamically allocates memory for n integers and initializes them to 0.
  • We use a pointer arr to access and manipulate the allocated memory.
  • After use, the memory is released using free(arr) to avoid memory leaks.

 

C Program: Dynamic array with pointers

Method 3: Using realloc() function

C

#include <stdio.h>

#include <stdlib.h>

 

int main() {

    int *arr;           // pointer for dynamic array

    int n, newSize, i;

 

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // allocate memory using calloc()

    arr = (int *)calloc(n, sizeof(int));

 

    if (arr == NULL) {

        printf("Memory not allocated.\n");

        return 1;

    }

 

    printf("Enter %d elements:\n", n);

    for (i = 0; i < n; i++) {

        scanf("%d", &arr[i]);

    }

 

    printf("Array elements before resizing:\n");

    for (i = 0; i < n; i++) {

        printf("%d ", arr[i]);

    }

 

    // ask for new size

    printf("\n\nEnter new size of the array: ");

    scanf("%d", &newSize);

 

    // reallocate memory using realloc()

    arr = (int *)realloc(arr, newSize * sizeof(int));

 

    if (arr == NULL) {

        printf("Memory reallocation failed.\n");

        return 1;

    }

 

    // input new elements if size increased

    if (newSize > n) {

        printf("Enter %d new elements:\n", newSize - n);

        for (i = n; i < newSize; i++) {

            scanf("%d", &arr[i]);

        }

    }

 

    printf("Array elements after resizing:\n");

    for (i = 0; i < newSize; i++) {

        printf("%d ", arr[i]);

    }

 

    // free memory

    free(arr);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter number of elements: 4
Enter 4 elements:
5 10 15 20

Array elements are:
5 10 15 20

Explanation

  1. calloc() initializes memory with zeros for the first n elements.
  2. The user enters initial elements.
  3. realloc() resizes the allocated memory to the new size:
    • If newSize > n, user can enter additional elements.
    • If newSize < n, extra elements are discarded.
  4. Memory is freed at the end using free().