Open In App

Difference between Arrays and Pointers

Improve
Improve
Like Article
Like
Save
Share
Report

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.



Last Updated : 07 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads