Pointer to an Array | Array Pointer

Pointer to an Array | Array Pointer

#include <iostream>
using namespace std;
 
int main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
int *ptr = arr;
 
cout <<"\n"<< ptr;
return 0;
}
 

This type of pointer is typically referred to as a “pointer to an array.” It allows you to work with entire arrays, which can be particularly useful for managing multidimensional data structures and passing arrays as function arguments.

Syntax

data_type (*var_name)[size_of_array];

Example

int (*ptr)[10];

To declare a pointer that can point to an array of 10 integers, you should use parentheses to ensure the proper order of operations. In this case, the type of ptr is indeed a “pointer to an array of 10 integers,” and it’s essential to define it correctly for proper memory manipulation and data access.

Program 1 – Difference Between Pointer to an Integer and Pointer to Array of integers

Whereas, the pointer points to the Oth element of the array and the pointer that pointer that points to the whole array are both different. The program below illustrates that:

// C++ program to understand difference between 
// pointer to an integer and pointer to an
// array of integers. 
#include <iostream>
using namespace std;
int main()
{
    // Pointer to an integer
    int *p; 
     
    // Pointer to an array of 5 integers
    int (*ptr)[5]; 
    int arr[5];
     
    // Points to 0th element of the arr.
    p = arr;
     
    // Points to the whole array arr.
    ptr = &arr; 
     
    cout << "p =" << p <<", ptr = "<< ptr<< endl;
    p++; 
    ptr++;
    cout << "p =" << p <<", ptr = "<< ptr<< endl;
     
    return 0;
}

Output

p = 0x7fff4f32fd50, ptr = 0x7fff4f32fd50
p = 0x7fff4f32fd54, ptr = 0x7fff4f32fd64

We can analyze that p is the integer and, ptr is the pointer, to an array of 5 integers.

Program 2 – To illustrate the size of an Array

When you dereference a pointer to an array in C, you get the base address (i.e., the address of the first element) of the array to which the pointer points. This is because the name of an array in C represents its base address. So, dereferencing a pointer to an array effectively gives you the base address of that array, allowing you to work with the array’s elements.

// C++ program to illustrate sizes of
// pointer of array
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int arr[] = { 3, 5, 6, 7, 9 };
    int *p = arr;
    int (*ptr)[5] = &arr;
     
    cout << "p = "<< p <<", ptr = " << ptr << endl;
    cout << "*p = "<< *p <<", *ptr = " << *ptr << endl;
     
    cout << "sizeof(p) = "<< sizeof(p) <<
            ", sizeof(*p) = " << sizeof(*p) << endl;
    cout << "sizeof(ptr) = "<< sizeof(ptr) <<
        ", sizeof(*ptr) = " << sizeof(*ptr) << endl;
    return 0;
}

Output

p = 0x7ffde1ee5010, ptr = 0x7ffde1ee5010
*p = 3, *ptr = 0x7ffde1ee5010
sizeof(p) = 8, sizeof(*p) = 4
sizeof(ptr) = 8, sizeof(*ptr) = 20

FAQ- Pointer to an Array | Array Pointer

Q1. How do you assign a pointer to an array of pointers?

Ans. The statement int *pa = &a[0]; assigns the memory address of the first element, a[0], in the array a to the pointer, which is of type int. This is a common way to initialize a pointer to point to the first element of an array. It allows you to work with the array’s elements using the pointer.

Q2. How to use pointer to pointer in an array in C?

Ans. #include<stdio.h>
void main ()
{
int a = 10;
int *p;
int **pp;
p = &a; // pointer p is pointing to the address of a.
pp = &p; // pointer pp is a double pointer pointing to the address of pointer p

Q3. Is the ptr a pointer to an array?

Ans. ptr is a pointer to an entire array. When you increment it by 1, it moves to the next block of 5 elements if it’s an array of 5 elements.
*ptr is a pointer to the first element of the array. When you increment it by 1, it moves to point to the second element within the array. This is because *ptr is equivalent to ptr[0], and incrementing it essentially accesses the next element in the array.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment