Pointer Vs Array In C

Arrays are commonly employed for holding data items or values that share a similar type or nature. One limitation of arrays is their inability to store data items or values of varying natures, which is viewed as a notable drawback of this data structure.

Syntax:

Example

int mark[5] = {19, 10, 8, 17, 9};

Definition of the pointers

Apart from arrays, when considering pointers, they are commonly employed to hold the memory location of another variable rather than the variable's actual value. Nonetheless, pointers can also be utilized to access an entire array through pointer arithmetic. Furthermore, this technique can significantly expedite the array access process.

Syntax:

The pointer variable can be declared as demonstrated here:

Example

//Declaration in C++
type * name;

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:

  • 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.
  • 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 thaLogic Practices to an array.
  • 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.
  • Arrays are allocated at compile-time, while pointers are allocated at runtime
  • 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:
  • The "sizeof" operator

When employing the "sizeof(array)" function, it provides the total memory occupied by all elements within the array. Conversely, when applying it to a pointer, such as "sizeof(pointer)", it solely indicates the memory consumed by the pointer variable and not the data it points to.

Comparison Chart

Let's examine a brief comparison table to grasp the distinctions between arrays and pointers more easily:

Basis for Comparison Pointer Array
Declaration //In C++type * var_name; //In C++type varname[size];//In Java.type var-name[ ];varname = 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 guide, we have outlined the fundamental variances between Arrays and Pointers. Consequently, once you have absorbed the contents of this article, we trust that any uncertainty you may have had regarding Pointers and arrays will be dispelled permanently.

Input Required

This code uses input(). Please provide values below: