• A A

Home > Technology > IT > Database > Pointer and Array Compared

Difference Between Pointer and Array

Pointer vs Array

A pointer is a data type that holds a reference to a memory location (i.e. a pointer variable stores an address of a memory location in which some data is stored). Arrays are the most commonly used data structure to store a collection of elements. Most programming languages provide methods to easily declare arrays and access elements in the arrays.

What is a Pointer?

A pointer is a data type that stores an address of a memory location in which some data is stored. In other words, a pointer holds a reference to a memory location. Accessing the data stored in the memory location that is referenced by the pointer is called dereferencing. When performing repetitive operations such as traversing trees/strings, table lookups, etc., using pointers would improve the performance. This is because dereferencing and copying pointers is cheaper than actually copying and accessing the data pointed by the pointers. A null pointer is a pointer that does not point to anything. In Java, accessing a null pointer would generate an exception called a NullPointerException.

What is an Array?

Shown in figure 1, is a piece of code typically used to declare and assign values to an array. Figure 2 depicts how an array would look like in the memory.

int values[5];

values[0]=100;

values[1]=101;

values[2]=102;

values[3]=103;

values[4]=104;

Figure 1: Code for declaring and assigning values to an array


100 101 102 103 104
Index:    0 1 2 3 4

Figure 2: Array stored in the memory

Above code defines an array that can store 5 integers and they are accessed using indices 0 to 4. One important property of an array is that, the entire array is allocated as a single block of memory and each element gets its own space in the array. Once an array is defined, its size is fixed. So if you are not sure about the size of the array at compile time, you would have to define a large enough array to be in the safe side. But, most of the times, we are actually going to use less number of elements than we have allocated. So a considerable amount of memory is actually wasted. On the other hand if the “large enough array” is not actually large enough, the program would crash.

What is the difference between Pointers and Arrays?

A pointer is a data type that stores an address of a memory location in which some data is stored, while Arrays are the most commonly used data structure to store a collection of elements. In C programming language, array indexing is done using pointer arithmetic (i.e. the ith element of the array x would be equivalent to *(x+i)). Therefore in C, set of pointers that point to a set of memory locations that are consecutive, can be thought of as an array. Further, there is a difference in how the sizeof operator operates on pointers and arrays. When applied to an array, sizeof operator will return the entire size of the array, whereas when applied to a pointer, it would return just the size of the pointer.


email

Related posts:

  1. Difference Between Integer and Pointer
  2. Difference Between Arrays and Arraylists
  3. Difference Between Arrays and Linked Lists
  4. Difference Between Linear and Nonlinear Data Structures
  5. Difference Between Hashtable and Hashmap

Tags: , , , , , , , , , , , , , ,

Copyright © 2010-2012 Difference Between. All rights reserved.Protected by Copyscape Web Plagiarism Detection
Terms of Use and Privacy Policy : Legal.
hit counters
eXTReMe Tracker
hit counters