Javatpoint Logo
Javatpoint Logo

Pointer vs array in C

Generally, in most cases, users assume that the pointer and array are both the same thing. However, it is not that simple because we will find some distinct differences if we look deeply at both. Therefore in this article, we will see whether the pointer and array are really the same or not.

Definition of array

Arrays are generally used for storing the same types of data items or values that are same in the nature. Although arrays cannot be used to store the data items or values that are not same in the nature, it is also considered a significant disadvantage of the arrays.

Syntax:

Definition of the pointers

Apart from the arrays, if we talk about the pointer, they are generally used for storing the memory address of the other variable instead of storing the actual value of the variable. However, we can also use the pointer to access the whole array by using the pointer arithmetic. In addition, it also even makes the accessing array process much faster.

Syntax:

The pointer variable can be as declared as shown below:

What are the main differences between pointers and arrays?

Apart from what we have discussed above, the key differences can be found while implementing the pointer and array. For example, when the arrays are implemented, the fixed size of the memory block is allocated. On the other hand, where the pointers are implemented, the memory is dynamically allocated. Thus, allocating the memory in both the pointers and arrays can be considered the key difference. However, it is not the only difference that lies between the arrays and pointer because some other differences also do exist that are as follows:

  1. An array usually stores the variables ofsimilar data types, and the data types of the variables must match the type of array. However, the pointer variable stores the address of a variable of a type similar to a type of pointer variable type.
  2. We can generate an array of pointers, i.e. array whose variables are the pointer variables. On the other hand, we can also create a pointer that points to an array.
  3. In general, arrays are static, which means once the size of the array is declared, it cannot be resized according to users requirements. While on the other hand, pointers are dynamic, which means the memory allocated can be resized later at any point in time.
  4. Arrays are allocated at compile-time, while pointers are allocated at runtime
  5. If we talk about the size of an array, it usually depends on the number of variables are stored in it. While in the case of the pointer variable, it stores the address of the variable only. To understand it more clearly, you can consider the following given examples:
  6. The "sizeof" operator

If we use the "sizeof(array)", it will return the amount of memory used by all elements stored in the array. However, if we use the it for the pointer, e.g. "sizeof(pointer)", it only returns the amount of memory used by the pointer variable itself.

Comparison Chart

Lets us see the quick comparison chart to understand the difference between both arrays and pointers in an easier manner:

Basis for Comparison Pointer Array
Declaration //In C++type * var_name; //In C++type var_name[size];//In Java.type var-name[ ];var_name = new type[size];
Working It generally stores the address of another variable of the same data type as the pointer variable's datatype. Array usually stores the value of the variable of the same datatype.
Generation A pointer to an array can be generated. An array of pointers can be generated.
Storage Pointers are specially designed to store the address of variables. A normal array stores values of variables, and pointer array stores the address of variables.
Capacity Usually, arrays can store the number of elements the same size as the size of the array variable. A pointer variable can store the address of only one variable at a time.

Conclusion

In this article, we have explained the key differences between Array and Pointers. Therefore, after reading this article, we hope that your confusion about pointer and array will be cleared forever.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA