Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Database /Difference Between Pointer and Array

Difference Between Pointer and Array

June 28, 2011Posted byIndika

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.

Related posts:

数组和Arraylist的区别s Difference Between Arrays and Linked Lists Difference Between Linear and Nonlinear Data StructuresDifference Between Linear and Nonlinear Data Structures Difference Between Stack and Heap Difference Between Exception and Error

Filed Under:DatabaseTagged With:array indexing,arrays,data structure,data type,dereferencing,异常,null pointer,Null Pointer Exception,null pointer in Java,NullPointerException,pointer arithmetic,Pointers,referencing,size of operator,sizeof operator

About the Author:Indika

Indika, BSc.Eng, MSECE Computer Engineering, PhD. Computer Science, is an Assistant Professor and has research interests in the areas of Bioinformatics, Computational Biology, and Biomedical Natural Language Processing.

Leave a ReplyCancel reply

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

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

What is the Difference Between Amyloidosis and Sarcoidosis

What is the Difference Between Amyloidosis and Sarcoidosis

Difference Between Iron and Ferritin

Difference Between Wildebeest and Buffalo

Difference Between Spring and Hibernate

Difference Between Spring and Hibernate

Difference Between Jules Verne and H.G. Wells

Latest Posts

  • What is the Difference Between Siamese and Himalayan Cats
  • What is the Difference Between RAC and Biofloc
  • What is the Difference Between Yolk Sac and Fetal Pole
  • What is the Difference Between Type I and Type IV Hypersensitivity
  • What is the Difference Between Pixie and Bob Haircut
  • What is the Difference Between Somatic and Visceral Pain
  • Home
  • Vacancies
  • About
  • Request Article
  • 联系我们

Copyright © 2010-2018Difference Between. All rights reserved.Terms of Useand Privacy Policy:Legal.