Difference between pointer and array in C

Difference between pointer and array in C?

Array and pointer have a close relationship but both are different concepts in C programming. In this blog post, I will discuss the difference between pointer and array ( pointer vs array ).

Before going to see the difference between pointer and array let us see the introduction of the array and pointer. If you are new in C programming, I have already written a brief article on pointer and array you can see.

 

What is Array in C?

An array is essentially a collection of elements. The data type of all elements must be the same and store at the contiguous memory location. So each array can store only one type of data. At the time of the array declaration, you must specify the type of data with the array name.

In array, we can access the elements using an index in square brackets. The index of the array always starts with 0. It means if you want to get the first element of the array then the index must be 0.

Note: In array first element at the lowest address and last element at the highest address.

 

Syntax of a single-dimensional array in C:

Data_Type  Array_Name [size];

 

for example, if you want to create an array of 5 integers, you have to declare an array as below expression. You can take an array name as your choice.

int arr[5];

 

Let see an example where I am creating an array and printing the array elements.

#include <stdio.h>

int main()
{
    int arr[5] = {1,2,3,4,5};
    int index = 0;

    for(index = 0; index < 5; ++index)
    {
        printf("arr[%d] = %d\n",index,arr[index]);
    }

    return 0;
}

Output:

arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5

 

What is Pointer in C?

A pointer is similar to a variable but the difference is that pointers are store the address of a location in memory and variable stored the value. In other words, we can say, a pointer is used to reference a location in the memory.

When we have used a pointer to store the address in the memory than using the dereferencing techniques we can also get the value from the address which is stored by the pointer.

Syntax of a pointer in C:

Declaration of a pointer is very important because at the time of declaration you define the capability of the pointer. Every pointer has the data types (pre-defined or user-defined) and names followed by an asterisk (*). Asterisk is a unary operator.

Data_Type * Pointer_Name;

 

Let’s see the below-mentioned example to understand the declaration of a pointer.

char *cPtr // pointer to a character
int *iPtr; // pointer to an integer
float *fPtr; // pointer to a float
double *dPtr; // pointer to a double

 

Difference between array and pointer in C:

Array and pointer are different from each other. Below I am mentioning some points which describe the difference between array and pointer in C language.

1. An array is a collection of elements of similar data types whereas pointer is a variable that store the address.

2.  Array element store at contiguous memory location whereas pointer can store one address at a time.

3.  When we use the sizeof the operator with the array it gives the total number of bytes which used by the elements whereas pointer only gives the size of the pointer.

#include <stdio.h>

int main()
{
    int arr[] = {10, 20, 30, 40, 50, 60};
    int *ptr = NULL;

    // 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 4

 

4.  Array static in nature means you could not resize the size of the array whereas with a pointer you can change the size of allocated memory at any point in time. But with the new compiler (After C99)  you can use the variable for size of the array but the value of the variable should be the integral positive number.

You can also check below Articles,

 

5. An array is fully controlled by the scope. It will allocate the needed memory correctly and when the variable goes out of the scope the memory will be automatically free.  Whereas problem with pointer is that if you create a local pointer which points to a dynamic memory and you forget to free it, It creates a memory leak.

Click to read more about memory leak: Dangling pointer and memory leak.

 

6. Another important difference between array and pointer is that we can increment the pointer but we can not create the increment the array.

array using pointer

 

You want to learn more about C Pointers, you can check the below articles.