Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
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.
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.
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.
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.