Table of contents
1.
Introduction
2.
Arrays
2.1.
Declaring Arrays
2.2.
Initializing Arrays
3.
Pointers
3.1.
Declaration and initialization of pointers
4.
Difference between Arrays and Pointers
5.
Frequently Asked Questions
5.1.
Is an array a data type?
5.2.
Is an array a primitive type in Java?
5.3.
Is it possible to add pointers to each other?
5.4.
What will be returned if we try to access an array variable outside its size?
5.5.
How are 2D arrays stored in C?
5.6.
Are pointers an array?
6.
Conclusion
Last Updated: Jun 5, 2024
Medium

Difference between Arrays and Pointers

Author SAURABH ANAND
3 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Array and pointer have a close relationship. Still, both are different concepts in C programming. A set of items stored in contiguous memory locations is called an array. In comparison, a variable whose value is the address of another variable is referred to as a pointer. 

This blog will show the concept of arrays and pointers and the difference between arrays and pointers.

Also Read, loop and while loop

Arrays

A set of items stored in contiguous memory locations is called an array. The objective is to group items of the same data type. This makes calculating the position of each element easy by simply adding an offset to a base value, such as the memory location of the array's first element. Index 0 is the starting point in an array, and the offset is the difference between the two indices.

Declaring Arrays

Now let us see how we declare an array. The basic syntax of array declaration is given below.

type arrayName [ arraySize ];

First, we define the type of array. It can be of type "int", "char", "float", etc. 

Secondly, we give a unique name to our array. The name of the array must not be any reserved keywords.

Thirdly, we specify the size of the array.

You can implement on an online C editor for better practice.

Initializing Arrays

Like any other variables, arrays can be initialized when they are declared. After the equals sign, put the initialization data in curly brackets {}.

An array can be partially initialized by supplying fewer data items than the array's size. The remaining array elements will be set to zero automatically.

The dimension of an array is not required if it is to be entirely initialized. The compiler will automatically resize the array to fit the initialized data.

Examples: 

  • char name1[ ] = { 'f', 'e', 'b' };
  •  int arr[]={1,3,45,2};

Pointers

A variable whose value is the address of another variable is referred to as a pointer. Like any variable or constant, a pointer must be declared before it can be used. Programs can imitate call-by-reference as well as generate and manipulate dynamic data structures using pointers.

General visualization of pointers

Source: https://www.scaler.com/topics/pass-by-value-and-call-by-reference-in-java/

Declaration and initialization of pointers

The general syntax for declaring pointers is:

datatype *var; 

Here, the data type is the pointer's base type; it must be a valid type, and *var is the name of the pointer variable. The asterisk we used to declare a pointer is the same asterisk you use for multiplication. However, in this line, the asterisk indicates a variable as a pointer.

int *im; // pointer to an integer

double *dm; // pointer to a double

float *fx; // pointer to a float

char *cz // pointer to character

You can also read about the dynamic arrays in c, C Static Function, And Tribonacci Series

Must Read Array of Objects in Java

Difference between Arrays and Pointers

Let's look at some differences between arrays and pointers.

Array

Pointer

Array syntax data type array_name[data type] Pointer syntax data type *pointer_name
An array is a set of objects of the same type. Whereas a pointer is a variable that contains the address of another variable.
The number of variables that an array may hold is determined by its size. But a pointer variable can only store the address of one variable.
Arrays can be initialized while defining. For example arr[5] = {1,2,3,4,5}. In comparison, pointers cannot be initialized at the definition.
Arrays are static in nature. The array size cannot be resized(allocated or freed) as per user requirements during runtime. Pointers, on the other hand, are dynamic in nature. The memory allocation can be allocated or freed at any point in time.
Arrays are allocated at compile time. Pointers are allocated at runtime.
We can not increment the array. We can increment the pointer.
The scope has complete control over an array. It will correctly allocate the required memory, and when the variable is no longer in scope, the memory will be automatically released. Pointers can cause a memory leak if we build a local pointer that points to dynamic memory and then forgets to release it.
Java has support for arrays. But, Java doesn't have pointers; Java has references.
Memory allocation of an array is sequential. Memory allocation is random.
The assembly code of the array is different from a pointer. The assembly code of the pointer is different from an array.


Also Read About, Sum of Digits in C and  Short int in C Programming

Frequently Asked Questions

Is an array a data type?

In the database dictionary, the array data type is a compound data type. Arrays are data structures that contain a list of items of the same data type that may be retrieved using an index (element) number. 

Is an array a primitive type in Java?

In Java, arrays aren't primitive datatypes. They are dynamically generated container objects. An array may be used to call all of the methods in the Object class. 

Is it possible to add pointers to each other?

No, it is not recommended to add two-pointers.

What will be returned if we try to access an array variable outside its size?

Some garbage value will be returned.

How are 2D arrays stored in C?

A 2D array is stored one row after another in the computer's memory. The memory address of the entire 2D array is determined by the address of the first byte of memory.

Are pointers an array?

Although pointers are not arrays, the pointer effectively becomes the array when an array is assigned to a pointer. The pointer will contain the address of the array's first element, and it will be able to read and write each element using pointer arithmetic and/or array-style indexing.

Read Also -  Difference between argument and parameter

Conclusion

In this article, we have extensively discussed the concept of arrays and pointers. We start with a brief introduction of arrays and pointers, then discuss their differences.

After reading about the attributes of a file, are you not feeling excited to read/explore more articles on the topic of file systems? Don't worry; Coding Ninjas has you covered. To learn, see array gate questions, array and pointers, and array of pointers.

To get a stronghold of the array concept, try solving the Sum Of Two Arrays problem.

Recommended read: AMD vs Intel

Refer to our Guided Path on Code 360 to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Live masterclass