The Wayback Machine - https://web.archive.org/web/20241004184413/https://www.geeksforgeeks.org/difference-between-array-and-pointers/
Open In App

Difference between Arrays and Pointers

Last Updated : 07 Aug, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The array and pointers are derived data types that have lots of differences and similarities. In some cases, we can even use pointers in place of an array, and arrays automatically get converted to pointers when passed to a function. So, it is necessary to know about the differences between arrays and pointers to properly utilize them in our program.

In this article, we will discuss the differences between the pointer and array and also how that affects the relationship between them. But first, let’s study them a little.

What is an Array?

An array is a data structure that represents a collection of elements of the same type stored in contiguous memory locations. It provides a way to store and access multiple values of the same data type using a single variable name.

  • It stores a homogeneous collection of data.
  • Its size can be decided by the programmer.
  • It provides random access.

The array is one of the most used data types in many programming languages due to its simplicity and capability.

What is a Pointer?

A pointer is a variable that holds the memory address of another variable. Pointers can be used for storing addresses of dynamically allocated arrays and for arrays that are passed as arguments to functions.

  • This size of the pointer is fixed and only depends upon the architecture of the system.

Pointers are one of the core concepts that provide a lot of unique and useful features in our program.

Difference between Arrays and Pointers

The following table lists the major differences between an array and a pointer:

S.No

Pointer

Array

          1.

It is declared as -:

*var_name;

It is declared as -:

data_type var_name[size];

2.

It is a variable that stores the address of another variable. It is the collection of elements of the same data type.

3.

We can create a pointer to an array. We can create an array of pointers.

4.

A pointer variable can store the address of only one variable at a time. An array can store a number of elements the same size as the size of the array variable.

5.

Pointer allocation is done during runtime. Array allocation is done during compile runtime.

6.

The nature of pointers is dynamic. The size of a pointer in C can be resized according to user requirements which means the memory can be allocated or freed at any point in time. The nature of arrays is static. During runtime, the size of an array in C cannot be resized according to user requirements.

Distinguishing Features of Arrays and Pointers

1. Behavior of sizeof operator

When used with arrays, sizeof operator returns the size in bytes occupied by the entire array whereas when used with pointers, it returns the size in bytes of the pointer itself regardless of the data types it points to.

Example:

C++




// 1st program to show that array and pointers are different
#include <iostream>
using namespace std;
  
int main()
{
    int arr[] = { 10, 20, 30, 40, 50, 60 };
    int* ptr = arr;
  
    // sizof(int) * (number of element in arr[]) is printed
    cout << "Size of arr[] " << sizeof(arr) << "\n";
  
    // sizeof a pointer is printed which is same for all
    // type of pointers (char *, void *, etc)
    cout << "Size of ptr " << sizeof(ptr);
    return 0;
}


C




// 1st program to show that array and pointers are different
#include <stdio.h>
  
int main()
{
    int arr[] = { 10, 20, 30, 40, 50, 60 };
    int* ptr = arr;
  
    // sizof(int) * (number of element in arr[]) is printed
    printf("Size of arr[] %ld\n", sizeof(arr));
  
    // sizeof a pointer is printed which is same for all
    // type of pointers (char *, void *, etc)
    printf("Size of ptr %ld", sizeof(ptr));
    return 0;
}


Output

Size of arr[] 24
Size of ptr 8

See the previous post on this topic for more differences.

Properties of Array that Make it Resemble Pointers

Although array and pointer are different concepts, the following properties of array make them look similar. 

1. Array name gives the address of the first element of the array

When we use the array name in the program, it implicitly represents the memory address of the first element in the array.

Example:

C++




// 1st program to show that array and pointers are different
#include <iostream>
using namespace std;
int main()
{
    int arr[] = { 10, 20, 30, 40, 50, 60 };
  
    // Assigns address of array to ptr
    int* ptr = arr;
    cout << "Value of first element is " << *ptr;
    return 0;
}


C




#include <stdio.h>
int main()
{
    int arr[] = { 10, 20, 30, 40, 50, 60 };
    // Assigns address of array to ptr
    int* ptr = arr;
    printf("Value of first element is %d", *ptr);
    return 0;
}


Output

Value of first element is 10

2. Array members are accessed using pointer arithmetic

The compiler uses pointer arithmetic to access the array elements. For example, an expression like “arr[i]” is treated as *(arr + i) by the compiler. That is why the expressions like *(arr + i) work for array arr, and expressions like ptr[i] also work for pointer ptr.

Example:

C++




#include <iostream>
using namespace std;
  
int main()
{
    int arr[] = { 10, 20, 30, 40, 50, 60 };
    int* ptr = arr;
    cout << "arr[2] = " << arr[2] << "\n";
    cout << "*(arr + 2) = " << *(arr + 2) << "\n";
    cout << "ptr[2] = " << ptr[2] << "\n";
    cout << "*(ptr + 2) = " << *(ptr + 2) << "\n";
    return 0;
}


C




#include <stdio.h>
  
int main()
{
   int arr[] = {10, 20, 30, 40, 50, 60};
   int *ptr = arr;
   printf("arr[2] = %d\n", arr[2]);
   printf("*(arr + 2) = %d\n", *(arr + 2));
   printf("ptr[2] = %d\n", ptr[2]);
   printf("*(ptr + 2) = %d\n", *(ptr + 2));
   return 0;
}


Output

arr[2] = 30
*(arr + 2) = 30
ptr[2] = 30
*(ptr + 2) = 30

3. Array parameters are always passed as pointers, even when we use square brackets

When an array is passed as a parameter to a function, the array name is converted to a pointer to its first element and the function receives the pointer that points to the first element of the array instead of the entire array.

C++




#include <bits/stdc++.h>
using namespace std;
  
int fun(int ptr[])
{
    int x = 10;
      
    // Size of a pointer is printed
    cout << "sizeof(ptr) = " 
         << (int)sizeof(*ptr) 
         << endl;
  
    // This allowed because ptr is a 
    // pointer, not array
    ptr = &x;
  
    cout <<"*ptr =  " << *ptr;
  
    return 0;
}
  
// Driver code
int main()
{
    int arr[] = { 10, 20, 30, 40, 50, 60 };
      
    // Size of a array is printed
    cout << "sizeof(arr) = " 
         << (int)sizeof(arr)
         << endl;
           
    fun(arr);
      
    return 0;
}
  
// This code is contributed by shivanisinghss2110


C




#include <stdio.h>
  
int fun(int ptr[])
{
    int x = 10;
  
    // size of a pointer is printed
    printf("sizeof(ptr) = %d\n", (int)sizeof(*ptr));
  
    // This allowed because ptr is a pointer, not array
    ptr = &x;
  
    printf("*ptr = %d ", *ptr);
  
    return 0;
}
  
// Driver code
int main()
{
    int arr[] = { 10, 20, 30, 40, 50, 60 };
      
    // size of a array is printed
    printf("sizeof(arr) = %d\n", (int)sizeof(arr));
    fun(arr);
    return 0;
}


Output

sizeof(arr) = 24
sizeof(ptr) = 4
*ptr = 10 

Related Articles

The pointer can be used to access the array elements, accessing the whole array using pointer arithmetic, makes the accessing faster. The main difference between Array and Pointers is the fixed size of the memory block. When Arrays are created the fixed size of the memory block is allocated. But with Pointers the memory is dynamically allocated. There are some other differences between an array and a pointer which are discussed below in the table.



Similar Reads

Difference between pointer to an array and array of pointers
Pointer to an array: Pointer to an array is also known as array pointer. We are using the pointer to access the components of the array. int a[3] = {3, 4, 5 }; int *ptr = a; We have a pointer ptr that focuses to the 0th component of the array. We can likewise declare a pointer that can point to whole array rather than just a single component of the
4 min read
Difference between Iterators and Pointers in C++ with Examples
In C++ programming, we have both pointers and iterators that are used in managing and manipulating data structures. There are many similarities between iterators and pointers in their ability to reference and dereference memory, but there are certain differences between the two. Understanding the difference is very important in C++ programming. Poi
4 min read
Near, Far and Huge Pointers in C
In older times, the intel processors had 16-bit registers but the address bus was 20-bits wide. Due to this, the registers were not able to hold the entire address at once. As a solution, the memory was divided into segments of 64 kB size, and the near pointers, far pointers, and huge pointers were used in C to store the addresses. There are old co
4 min read
Features and Use of Pointers in C/C++
Pointers store the address of variables or a memory location. Syntax: datatype *var_name; Example: pointer "ptr" holds the address of an integer variable or holds the address of memory whose value(s) can be accessed as integer values through "ptr" int *ptr; Features of Pointers: Pointers save memory space.Execution time with pointers is faster beca
2 min read
How to Declare and Initialize an Array of Pointers to a Structure in C?
Prerequisite: Structure in CArray in C In C language, arrays are made to store similar types of data in contiguous memory locations. We can make arrays of either primitive data types, like int, char, or float, or user-defined data types like Structures and Unions. We can also make arrays of pointers in C language. Today we will learn how to create
8 min read
Dangling, Void , Null and Wild Pointers in C
In C programming pointers are used to manipulate memory addresses, to store the address of some variable or memory location. But certain situations and characteristics related to pointers become challenging in terms of memory safety and program behavior these include Dangling (when pointing to deallocated memory), Void (pointing to some data locati
6 min read
Pointers and References in C++
In C++ pointers and references both are mechanisms used to deal with memory, memory address, and data in a program. Pointers are used to store the memory address of another variable whereas references are used to create an alias for an already existing variable. Pointers in C++ Pointers in C++ are a symbolic representation of addresses. They enable
5 min read
Function Pointers and Callbacks in C++
A callback is a function that is passed as an argument to another function. In C++, we cannot directly use the function name to pass them as a callback. We use function pointers to pass callbacks to other functions, store them in data structures, and invoke them later. In this article, we will discuss how to use function pointer to pass callbacks i
2 min read
Program to reverse an array using pointers
Prerequisite : Pointers in C/C++ Given an array, write a program to reverse it using pointers . In this program we make use of * operator . The * (asterisk) operator denotes the value of variable . The * operator at the time of declaration denotes that this is a pointer, otherwise it denotes the value of the memory location pointed by the pointer .
4 min read
Move all zeroes to end of array using Two-Pointers
Given an array of random numbers, Push all the zero’s of the given array to the end of the array. For example, if the given arrays is {1, 0, 2, 6, 0, 4}, it should be changed to {1, 2, 6, 4, 0, 0}. The order of all other elements should be the same.Examples: Input: arr[]={8, 9, 0, 1, 2, 0, 3} Output: arr[]={8, 9, 1, 2, 3, 0, 0} Explanation: Swap {0
6 min read
How many levels of pointers can we have in C/C++
Prerequisite: Pointer in C and C++, Double Pointer (Pointer to Pointer) in CA pointer is used to point to a memory location of a variable. A pointer stores the address of a variable and the value of a variable can be accessed using dereferencing of the pointer. A pointer is generally initialized as: datatype *variable name; This above declaration i
7 min read
Why do we need reference variables if we have pointers
Pointers: A pointer is a variable that holds memory address of another variable. A pointer needs to be de referenced with * operator to access the memory location it points to.References: A Reference can be called as a constant pointer that becomes de referenced implicitly. When we access the reference it means we are accessing the storage.Why do w
4 min read
Unusual behaviour with character pointers
In C++, cout shows different printing behaviour with character pointers/arrays unlike pointers/arrays of other data types. So this article will firstly explain how cout behaves differently with character pointers, and then the reason and the working mechanism behind it will be discussed. Example 1: C/C++ Code // C++ program to illustrate difference
5 min read
C++ Modify Pointers
Prerequisite: Pointer in C++ Pointers are an essential part of the C++ language. It allows us to store the memory address of a variable. One thing to note here is that if we change the value that a pointer is pointing to, then the value of the original variable also changes. Now, let's check the approach we are going to use. Approach: In the below
2 min read
How to declare a Two Dimensional Array of pointers in C?
A Two Dimensional array of pointers is an array that has variables of pointer type. This means that the variables stored in the 2D array are such that each variable points to a particular address of some other element. How to create a 2D array of pointers: A 2D array of pointers can be created following the way shown below. int *arr[5][5]; //creati
3 min read
Passing Pointers to Functions in C
Prerequisites: Pointers in CFunctions in C Passing the pointers to the function means the memory location of the variables is passed to the parameters in the function, and then the operations are performed. The function definition accepts these addresses using pointers, addresses are stored using pointers. Arguments Passing without pointer When we
2 min read
Pointers in Containers in C++ STL
Prerequisites: Pointers in C++Container in STL There are different containers in C++ having their own features where if are adding extra functionality of pointer it will contribute in a different way. Although not all of the containers are useful in this context so let's see a few of the containers which can be helpful. There are 5 containers that
5 min read
Passing Pointers to Functions In C++
Prerequisites: Pointers in C++Functions in C++ Passing Pointers to functions means declaring the function parameter as a pointer, at the function calling passing the address of the variable and that address will be stored by a parameter that is declared as a pointer. To change the value of any variable in the function we have to pass the address of
4 min read
What are Wild Pointers? How can we avoid?
Uninitialized pointers are known as wild pointers because they point to some arbitrary memory location and may cause a program to crash or behave unexpectedly. Example of Wild PointersIn the below code, p is a wild pointer. C/C++ Code // C program that demonstrated wild pointers int main() { /* wild pointer */ int* p; /* Some unknown memory locatio
2 min read
Why Does C Treat Array Parameters as Pointers?
In C, array parameters are treated as pointers mainly to, To increase the efficiency of codeTo save time It is inefficient to copy the array data in terms of both memory and time; and most of the time, when we pass an array our intention is to just refer to the array we are interested in, not to create a copy of the array. The following two definit
2 min read
Pointers vs References in C++
Prerequisite: Pointers, References C and C++ support pointers, which is different from most other programming languages such as Java, Python, Ruby, Perl and PHP as they only support references. But interestingly, C++, along with pointers, also supports references. On the surface, both references and pointers are very similar as both are used to hav
5 min read
C++ Pointers
Pointers are symbolic representations of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Iterating over elements in arrays or other data structures is one of the main use of pointers.  The address of the variable you're working with is assigned to the pointer variable that p
9 min read
Pointers vs Array in C++
Arrays and pointers are two derived data types in C++ that have a lot in common. In some cases, we can even use pointers in place of arrays. But even though they are so closely related, they are still different entities. In this article, we will study how the arrays and pointers are different from each other in C++. What is an Array?An array is the
3 min read
Applications of Pointers in C
Pointers in C are variables that are used to store the memory address of another variable. Pointers allow us to efficiently manage the memory and hence optimize our program. In this article, we will discuss some of the major applications of pointers in C. Prerequisite: Pointers in C. C Pointers ApplicationThe following are some major applications o
4 min read
Smart Pointers in C++
Prerequisite: Pointers in C++ Pointers are used for accessing the resources which are external to the program - like heap memory. So, for accessing the heap memory (if anything is created inside heap memory), pointers are used. When accessing any external resource we just use a copy of the resource. If we make any changes to it, we just change it i
8 min read
C Pointers
Pointers are one of the core components of the C programming language. A pointer can be used to store the memory address of other variables, functions, or even other pointers. The use of pointers allows low-level memory access, dynamic memory allocation, and many other functionality in C. In this article, we will discuss C pointers in detail, their
14 min read
Array of Pointers in C
In C, a pointer array is a homogeneous collection of indexed pointer variables that are references to a memory location. It is generally used in C Programming when we want to point at multiple memory locations of a similar data type in our C program. We can access the data by dereferencing the pointer pointing to it. Syntax: pointer_type *array_nam
6 min read
Program to Reverse a String using Pointers
Given a string, the task is to reverse this String using pointers. Examples: Input: GeeksOutput: skeeGInput: GeeksForGeeksOutput: skeeGroFskeeGApproach: This method involves taking two pointers, one that points at the start of the string and the other at the end of the string. The characters are then reversed one by one with the help of these two p
2 min read
How to Avoid Memory Leaks When Using a Vector of Pointers to Dynamically Allocated Objects in C++?
In C++, managing memory properly is very important to avoid memory leaks, especially when working with dynamically allocated objects. When using a std::vector of pointers to dynamically allocated objects, we need to ensure that all allocated memory is properly deallocated. In this article, we will learn different methods to avoid memory leaks in su
6 min read
Multidimensional Arrays in C - 2D and 3D Arrays
Prerequisite: Arrays in C A multi-dimensional array can be defined as an array that has more than one dimension. Having more than one dimension means that it can grow in multiple directions. Some popular multidimensional arrays are 2D arrays and 3D arrays. In this article, we will learn about multidimensional arrays in C programming language. Synta
10 min read