• Networking
  • Programming
  • DBMS
  • Operating System
  • Internet
  • Hardware
  • Software

Tech Differences

Know the Technical Differences

Difference Between Array and Pointer

array and pointerThere is a close relationship between array and pointer. There is a basic difference between an array and pointer is that an array is a collection of variables of a similar data type. In contrast, the pointer is a variable which is used for storing the address of another variable. The pointer can be used to access the array elements, accessing the whole array using pointer arithmetic, makes the accessing faster.

Furthermore, the other difference lies between the implementation of the array and pointer where the array are implemented when the fixed size of the memory is allocated. On the contrary, the pointers can be used for allocating the memory dynamically. There are some other differences between an array and a pointer which are discussed below in the comparison chart.

Content: Array Vs Pointer

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison Chart

Basis for ComparisonArrayPointer
Declaration//In C++
type var_name[size];
//In Java.
type var-name[ ];
var_name = new type[size];
//In C++
type * var_name;
WorkingStores the value of the variable of homogeneous datatype.Store the address of the another variable of same datatype as the pointer variable's datatype.
GenerationAn array of pointers can be generated.A pointer to an array can be generated.
Java SupportSupport the concept of array.Does not support pointers.
StorageA normal array stores values of variable and pointer array stores the address of variables.Pointers are specially designed to store the address of variables.
CapacityAn 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.

Definition of Array

An array is the collection of elements of the same data type, and all these elements are referred by a common name, which is the name of an array variable. A particular array element can be accessed by accessing the specific index of that array where that element is stored.

Arrays can be a one-dimensional array, a two-dimensional array, or multidimensional array. You can also refer our previous article, to understand the difference between One-dimensional and two-dimensional array. An array of pointers can also be generated i.e. an array containing all variable as a pointer variable.

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‘ denotes the name given to the array variable, ‘size‘ denote the capacity of an array variable, i.e. how many elements of ‘type’  can be stored in that array variable.

Methods of accessing the array

There are two methods of accessing an array, first ‘pointer arithmetic‘ and second ‘array indexing‘, of which ‘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 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 variable 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.

Definition of Pointer

The pointer is a variable that holds the memory address of another variable. The datatype of both, the pointer variable and the variable whose address is being assigned to a pointer variable, must be same.

Syntax

The pointer variable is as declared as follow.

//Declaration in C++
type * name;

Here, ‘type‘ is a datatype, ‘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 ‘&’ 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 datatype i.e. by 2 bytes, as it is an integer pointer, on increment, it will have to point next integer variable. 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

  1. 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.
  2. 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.
  3. Java supports array, but it does not support pointers.
  4. An array size decides the number of variables it can store. As against, a pointer variable can store the address of the only variable.

Note

Java does not support or strictly avoid pointers.

Conclusion

When we need to work on data elements of similar data type 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.

Related Differences:

  1. Difference Between One-Dimensional (1D) and Two-Dimensional (2D) Array
  2. Difference Between Array and Linked List
  3. Difference Between Array and Structure
  4. Difference Between Pointer and Reference
  5. Difference Between Clustered and Non-clustered index

Comments

  1. Yashpreet Singh says

    August 19, 2018 at 7:11 am

    I like the way defined everything in this site.
    Thank you

    Reply
  2. Joel says

    January 11, 2019 at 3:46 pm

    Good introduction

    Reply
  3. Rahul saha says

    January 21, 2019 at 6:20 pm

    very well explained.. good job!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Top 10 Differences

  • Difference Between OLTP and OLAP
  • Difference Between while and do-while Loop
  • Difference Between Guided and Unguided Media
  • Difference Between Preemptive and Non-Preemptive Scheduling in OS
  • Difference Between LAN, MAN and WAN
  • Difference Between if-else and switch
  • Difference Between dispose() and finalize() in C#
  • Difference Between for and while loop
  • Difference Between View and Materialized View
  • Difference Between Server-side Scripting and Client-side Scripting

Recent Addition

  • Difference Between Cache and Main Memory
  • Difference Between Modulation and Demodulation
  • Difference Between Static and Dynamic Memory Allocation
  • Difference Between VPN and DNS
  • Difference Between Maskable and Non-Maskable Interrupts

Categories

  • DBMS
  • Hardware
  • Internet
  • Networking
  • Operating System
  • Programming
  • Software

Copyright © 2022 · Tech Differences · Contact Us · About Us · Privacy