Cpp Programming Pointers With Arrays And Functions Complete Guide

 Last Update:2025-06-23T00:00:00     .NET School AI Teacher - SELECT ANY TEXT TO EXPLANATION.    8 mins read      Difficulty-Level: beginner

Understanding the Core Concepts of CPP Programming Pointers with Arrays and Functions

C++ Programming: Pointers with Arrays and Functions

1. Pointers and Arrays

In C++, arrays and pointers have a strong inherent relationship. When you declare an array, the variable name of the array can be treated as a pointer to the first element of the array. Understanding this relationship is crucial for managing memory effectively.

  • Array Declaration:

    int arr[5] = {10, 20, 30, 40, 50};
    

    Here, arr is the name of the array, and it also acts as a pointer to the first element arr[0].

  • Pointer to Array: You can explicitly declare a pointer to the first element of an array:

    int* ptr = arr; // ptr now points to arr[0]
    
  • Accessing Array Elements using Pointers: You can use pointer arithmetic to access elements of an array:

    std::cout << *(ptr + 1); // Output: 20
    
  • Pointer Arithmetic: Pointers can be incremented or decremented to navigate through array elements:

    std::cout << *(ptr);   // Output: 10
    std::cout << *(ptr+1); // Output: 20
    

2. Passing Arrays to Functions

When passing an array to a function, it decays into a pointer to its first element. This means that inside the function, the parameter is treated as a pointer, not as an array with a known size.

  • Example:
    void printArray(int* arr, int size) {
        for(int i = 0; i < size; i++) {
            std::cout << arr[i] << " ";
        }
    }
    
    int main() {
        int numbers[5] = {10, 20, 30, 40, 50};
        printArray(numbers, 5); // Passing the array and its size
        return 0;
    }
    
  • Passing Multidimensional Arrays: Similarly, multidimensional arrays passed to functions must be declared with their second dimension:
    void printMatrix(int matrix[][3], int rows) { // Second dimension must be known
        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < 3; j++) {
                std::cout << matrix[i][j] << " ";
            }
            std::cout << std::endl;
        }
    }
    
    int main() {
        int mat[2][3] = {{1, 2, 3}, {4, 5, 6}};
        printMatrix(mat, 2);
        return 0;
    }
    

3. Returning Pointers from Functions

Functions can return pointers to data. However, care must be taken to ensure that the data being pointed to exists for the duration the pointer is used.

  • Returning a Pointer to Dynamically Allocated Memory: A common use case is returning a pointer to dynamically allocated memory (using new).
    int* createArray(int size) {
        int* arr = new int[size]; // Allocate memory
        for(int i = 0; i < size; i++) {
            arr[i] = i * 10; // Initialize array
        }
        return arr; // Return pointer to array
    }
    
    int main() {
        int* myArray = createArray(5);
        for(int i = 0; i < 5; i++) {
            std::cout << myArray[i] << " ";
        }
        delete[] myArray; // Free allocated memory
        return 0;
    }
    

4. Pointers to Functions

In C++, pointers can also point to functions, allowing for more flexible and dynamic program design.

  • Function Pointer Declaration:

Online Code run

🔔 Note: Select your programming language to check or run code at

💻 Run Code Compiler

Step-by-Step Guide: How to Implement CPP Programming Pointers with Arrays and Functions

Example 1: Pointers and Arrays

Objective:

Learn how pointers and arrays are interrelated and how to manipulate them using pointers.

Code:

#include <iostream>

int main() {
    // Declare and initialize an array
    int arr[5] = {10, 20, 30, 40, 50};
    
    // Pointer to the first element of the array
    int* ptr = arr;
    
    // Accessing array elements using the pointer
    std::cout << "Array elements using pointer:" << std::endl;
    for (int i = 0; i < 5; i++) {
        // *(ptr + i) accesses the i-th element
        // Incrementing the pointer to move to the next element
        std::cout << "Element at index " << i << ": " << *(ptr + i) << std::endl;
    }
    
    // Using pointer arithmetic to access array elements
    for (int i = 0; i < 5; i++) {
        std::cout << "Element at index " << i << ": " << *(arr + i) << std::endl;
    }
    
    return 0;
}

Explanation:

  • int arr[5] initializes an array with 5 elements.
  • int* ptr = arr; sets ptr to point to the first element of the array arr.
  • *(ptr + i) accesses the i-th element by moving ptr by i positions in memory.
  • *(arr + i) also accesses the i-th element since arr itself acts like a pointer to its first element.

Example 2: Pass Array to Function Using Pointer

Objective:

Learn how to pass arrays to functions using pointers and manipulate them.

Code:

#include <iostream>

// Function prototype
void printArray(int* arr, int size);

int main() {
    // Declare and initialize an array
    int arr[5] = {10, 20, 30, 40, 50};
    
    // Pass the array to the function using a pointer
    printArray(arr, 5); // Here 'arr' is implicitly converted to '&arr[0]'
    
    return 0;
}

// Function to print array elements
void printArray(int* arr, int size) {
    std::cout << "Array elements:" << std::endl;
    for (int i = 0; i < size; i++) {
        std::cout << "Element at index " << i << ": " << arr[i] << std::endl;
    }
}

Explanation:

  • int arr[5] is the array we want to pass.
  • printArray(arr, 5); passes the array to a function. In C++, when an array is passed to a function, it decays to a pointer to its first element.
  • void printArray(int* arr, int size) is the function that receives the pointer and array size.
  • Inside the function, array elements can be accessed using arr[i] or *(arr + i).

Example 3: Modify Array Elements in Function Using Pointer

Objective:

Learn how to modify array elements within a function using pointers.

Code:

#include <iostream>

// Function prototype
void modifyArray(int* arr, int size);

int main() {
    // Declare and initialize an array
    int arr[5] = {10, 20, 30, 40, 50};
    
    // Print original array
    std::cout << "Original Array:" << std::endl;
    for (int i = 0; i < 5; i++) {
        std::cout << "Element at index " << i << ": " << arr[i] << std::endl;
    }
    
    // Pass the array to the function to modify its elements
    modifyArray(arr, 5);
    
    // Print modified array
    std::cout << "Modified Array:" << std::endl;
    for (int i = 0; i < 5; i++) {
        std::cout << "Element at index " << i << ": " << arr[i] << std::endl;
    }
    
    return 0;
}

// Function to modify array elements
void modifyArray(int* arr, int size) {
    for (int i = 0; i < size; i++) {
        // Modify each element
        arr[i] *= 2; // Multiply each element by 2
    }
}

Explanation:

  • modifyArray(arr, 5) passes the array to the function modifyArray.
  • Inside modifyArray, we modify each element by doubling its value.
  • Since arrays are passed by reference using pointers, the original array in the main function is modified directly.

Example 4: Dynamically Allocate Array Using Pointers

Objective:

Learn how to dynamically allocate arrays using pointers and manage them.

Code:

You May Like This Related .NET Topic

Login to post a comment.