Array and pointer both are programming elements. An array is a data structure that holds variables of the same data type. On the other hand, the pointer is a kind of variable that holds the address of another variable which has the same data type as of pointer variable. We can even use a pointer to access the array elements. Accessing the whole array using pointer arithmetic, makes the accessing faster.
While implementing an array a fixed memory size is allocated to array elements. Conversely, while implementing pointers memory is allocated to them dynamically. We will also try to identify some other differences between array and pointer in the section ahead.
Content: Array Vs Pointer
Comparison Chart
Basis for Comparison | Array | Pointer |
---|---|---|
Declaration | //In C++ type var_name[size]; //In Java. type var-name[ ]; var_name = new type[size]; | //In C++ type * var_name; |
Working | Stores the value of the variable of homogeneous datatype. | Store the address of the another variable of same datatype as the pointer variable's datatype. |
Generation | An array of pointers can be generated. | A pointer to an array can be generated. |
Java Support | Support the concept of array. | Does not support pointers. |
Storage | A normal array stores values of variable and pointer array stores the address of variables. | Pointers are specially designed to store the address of variables. |
Capacity | An array can store the number of elements, mentioned in the size of array variable. | A pointer variable can store the address of only one variable at a time. |
What is Array?
An array is a data structure that holds a number of variables that are of the same data type. We can refer to all the variables of an array with a common name that is by the name of that array variable. If you want to access a particular element from an array then you can access it using the specific index of the array where the element is stored.
Arrays can be a one-dimensional array, two-dimensional array, or multidimensional array. You can also refer to our previous content, to understand the difference between One-dimensional and two-dimensional array. We can also generate an array of pointers, which is an array that holds pointer variables.
Syntax
In ‘C++’ the arrays are statically allocated whereas, in ‘Java’ the arrays are dynamically allocated.
// In C++ type var_name[size]; //In Java. type var-name[ ]; var_name = new type[size];
Here the ‘type’ denotes the data type of an array variable, ‘var_name’. The ‘var_name’ denotes the name given to the array variable. The ‘size’ denotes the capacity of an array variable, i.e. how many elements of data type ‘type’ can be stored in that array variable.
Methods of Accessing the Array
There are two methods of accessing an array.
- Pointer arithmetic
- Array indexing
Out of these two, the ‘pointer arithmetic’ is faster.
//accessing using pointer arithmetic Void display_array(int * S) { while(*s) { cout<<"value is"<<*s; *s++; } }
The ‘pointer arithmetic’ would work faster as compared to ‘array indexing’, i.e. accessing an array variable using its index. If you need to pass an array of pointers into a function, it can be done by using the same method you use to pass a normal array i.e. directly call the function with the name of the array, without any index.
Example
Let us understand it with the example
//Declaring the array of pointers. int *p[10];
Here, it shows that ‘p’ is an array of integer type, it will hold the address of 10 variables of integer type. Let us pass the above pointer array into a function display( ).
display(p); //Call the function display. void display(int *d[]) { //Function receving the pointer array. for(int i=0; i<10; i++) { cout<<"index"<<i; cout<<"value at the address stored at this index"<<*p[i]; } }
This function will display the values, present in the variables, whose addresses are stored in this pointer array sequentially.
What is Pointer?
The pointer is a variable that holds the memory address of another variable. The data type of both, the pointer variable and the variable whose address is being assigned to a pointer variable must be the same.
Syntax
The pointer variable is as declared as follows.
//Declaration in C++ type * name;
Here, ‘type‘ is a datatype, and ‘name‘ is the name of the pointer variable. The ‘type’ define what kind of variable’s address can be stored in the pointer variable. For example, the integer pointer will store the address of the integer variable.
There are two pointer operators ‘*’ and ‘&’. The operator ‘*’ returns the value located at the address, which is stored in the variable followed by the ‘*’ sign. The ‘&’ operator returns the address of the variable followed by the ‘&’ sign.
//for example int b=10 int a= &b; //Here the address of 'b' is stored in the variable 'a'. //let's the address of 'b' is 2000, so now 'a=2000'. int c=*a; //Here, the integer pointer variable '*a' will return the value which is located at the address stored in 'a' .ie. 'c=10'.
Arithmetic operators used with the pointers
There are only two arithmetic operators you can use on pointers i.e. addition and subtraction. If you apply increment on an integer pointer variable, it will be incremented by the size of the datatype i.e. by 2 bytes, as it is an integer pointer, on increment, it will have to point next integer variable. The same is the case with decrement.
// 'p' is a integer pointer containg value 2000. p++; // now 'p=2002'. p--; //now 'p' again contain 2000 as decremented by two bytes.
Key Differences Between Array and Pointer
- An array stores the variables of similar data types and the data types of the variables must match the type of array. Conversely, 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 create a pointer that points to an array.
- Java supports arrays, but it does not support pointers.
- An array stores variables of primitive data type and a pointer array stores the address of variables of the same data type. A pointer store address of a variable of a similar datatype.
- An array size decides the number of variables it can store. As against, a pointer variable can store the address of the only variable.
- A contiguous memory is allocated to the array of elements of an array. However, a random memory is allocated to a pointer variable.
Conclusion
When we need to work on data elements of similar data types then, instead of working separately on variables, we can create an array of those variables of similar data types and then operate on it. Pointers are necessary for some program, it gives tremendous power, but unfortunately, if a pointer contains an incorrect value, it will be the most difficult bug to find.
Yashpreet Singh says
I like the way defined everything in this site.
Thank you
Joel says
Good introduction
Rahul saha says
very well explained.. good job!