Difference Between Array And Pointer

SHARE

What Is An Array?

Arrays are a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection a collection of data, but is often more useful to think of an array as a collection of variables of same type. Arrays are commonly used in computer programs to organize data so that a related set of values can be easily sorted or searched.

For example, a search engine may use an array to store Web pages found in a search performed by the user. When displaying the results, the program will output one element of the array at a time. This may be done for a specified number of values or until all the values stored in the array have been output. While the program could create a new variable for each result found, storing the results in an array is much more efficient way to manage memory.

Most important list of array features includes:

  • Copying and cloning
  • Insertion and deletion
  • Searching and sorting

What You Need To Know About Array

  • An array is a single, pre-allocated chunk of contagious element (all the same type), fixed in size and location.
  • They are static in nature. Once the memory is allocated, it cannot be resized or freed dynamically according to users requirement.
  • Arrays are allocated at compile time i.e at the time when programmer is writing program.
  • An array size decides the number of variables it can store.
  • Array can be initialized at definition. Example int num={2, 4, 5}
  • The assembly code of Array is different than pointer.
  • Memory allocation is sequential.
  • Array is a group of elements.
  • Types of array include single dimensional array, two-dimensional array and multidimensional array.
  • Java supports the concept of array.

What Is A Pointer?

A pointer is a programming language object that stores a memory address. This can be that of another value located in computer memory or in some cases that of memory mapped computer hardware. Unlike other variables that hold values of a certain type, pointer holds the address of a variable. For example, an integer variable holds an integer value; however an integer pointer holds the address of an integer variable.

Using pointers significantly improves performance for repetitive operations like traversing iterable data structures e.g strings, lookup tables, control tables and tree structures. In particular, it is often much cheaper in time and space to copy and dereferences pointers than it is to copy and access the data to which the pointers point.

Uses Of Pointer

  • They are primarily used for constructing references, which in turn are fundamental to constructing nearly all data structures, as well as in passing data between different parts of a program.
  • Pointers can also be used to allocate and de-allocate dynamic variables and arrays in memory.
  • Pointers are used to pass parameters by reference.
  • In data structures such as linked lists, pointers are used as references to explicitly tie one piece of the structure to another.

What You Need To Know About Pointer

  • A pointer is a place in memory that keeps the address of another place inside.
  • Pointer is dynamic in nature. The memory allocation can be resized or freed later at any point in time.
  • Pointers contain memory addresses as their values, so they can also be used to access and manipulate data stored in memory.
  • Pointers are allocated at runtime i.e after executing program.
  • A pointer variable can store the address of only one variable.
  • Pointer can’t be initialized at the definition.
  • The assembly code of pointer is different than array.
  • Memory allocation is random.
  • Pointer is not a group of elements. It is a single variable.
  • Java does not support pointers.

Also Read: Difference Between Array And Linked List Data Structures

Difference Between Array And Pointer In Tabular Form

BASIS OF COMPARISON ARRAY POINTER
Description An array is a single, pre-allocated chunk of contagious element (all the same type), fixed in size and location.   A pointer is a place in memory that keeps the address of another place inside.  
Nature They are static in nature. Once the memory is allocated, it cannot be resized or freed dynamically according to users requirement.   Pointer is dynamic in nature. The memory allocation can be resized or freed later at any point in time.  
Allocation Arrays are allocated at compile time i.e at the time when programmer is writing program.   Pointers are allocated at runtime i.e after executing program.  
Initialization Array can be initialized at definition. Example int num={2, 4, 5}   Pointer can’t be initialized at the definition.  
Number Of Variables Stored An array size decides the number of variables it can store.   A pointer variable can store the address of only one variable.  
Assembly Code The assembly code of Array is different than pointer.   The assembly code of pointer is different than array.    
Memory Allocation Memory allocation is sequential.   Memory allocation is random.  
Existence Array is a group of elements.   Pointer is not a group of elements. It is a single variable.  
Support On Java Java supports the concept of array.   Java does not support pointers.  
Syntax Array syntax Data type arrayName [data type]; Pointer syntax-data type *variable_name;

Also Read: Difference Between Structure And Union

Comments are closed.