Skip to content
Related Articles

Related Articles

Improve Article

Difference between pointer and array in C?

  • Difficulty Level : Easy
  • Last Updated : 03 Jun, 2021
 

Pointers are used for storing address of dynamically allocated arrays and for arrays which are passed as arguments to functions. In other contexts, arrays and pointer are two different things, see the following programs to justify this statement. 
Behavior of sizeof operator 

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

Assigning any address to an array variable is not allowed. 

C++




// IInd program to show that array
// and pointers are different
#include <iostream>
using namespace std;
 
int main()
{
   int arr[] = {10, 20}, x = 10;
   int *ptr = &x; // This is fine
   arr = &x;  // Compiler Error
   return 0;
}
 
// This code is contributed by Shubhamsingh10

C




// IInd program to show that array and pointers are different
#include <stdio.h>
 
int main()
{
   int arr[] = {10, 20}, x = 10;
   int *ptr = &x; // This is fine
   arr = &x;  // Compiler Error
   return 0;
}

Output: 

 Compiler Error: incompatible types when assigning to 
              type 'int[2]' from type 'int *' 

See the previous post on this topic for more differences. 
Although array and pointer are different things, following properties of array make them look similar. 

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

Consider the following program for 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

Array members are accessed using pointer arithmetic. 
Compiler uses pointer arithmetic to access array element. 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.

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

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

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 

Please refer Pointer vs Array in C for more details.

This article is contributed by Abhay Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

 

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.



My Personal Notes arrow_drop_up
Recommended Articles
Page :