Pointer Vs Array in C
Pointer Vs Array in C
Pointer vs Array in C
Arrays and Pointers are two important language constructs in C, associated with
each other in many ways. In many cases, the tasks that you perform with a pointer
can also be performed with the help of an array.
However, there are certain conceptual differences between arrays and pointers. Read
this chapter to understand their differences and comparative advantages and
disadvantages.
Arrays in C
In a C program, an array is an indexed collection of elements of similar type, stored
in adjacent memory locations.
int arr[5];
The array can be initialized along with the declaration, with the elements given as
a comma-separated list inside the curly brackets. Mentioning its size is optional.
Example of an Array
The following example shows how you can traverse an array with indexed subscripts
−
#include <stdio.h>
https://www.tutorialspoint.com/cprogramming/c_pointer_vs_array.htm 1/5
6/16/24, 1:14 PM Pointer vs Array in C
int i;
return 0;
}
Output
When you run this code, it will produce the following output −
Pointers in C
C allows you to access the memory location of a variable that has been randomly
allocated by the compiler. The address−of operator (&) returns the address of the
variable.
A variable that stores the address of another variable is called a pointer. The type of
the pointer must be the same as the one whose address it stores.
To differentiate from the target variable type, the name of the pointer is prefixed
with an asterisk (*). If we have an int variable, its pointer is declared as "int *".
int x = 5;
int *y = &a;
https://www.tutorialspoint.com/cprogramming/c_pointer_vs_array.htm 2/5
6/16/24, 1:14 PM Pointer vs Array in C
Note: In case of an array, the address of its 0th element is assigned to the pointer.
In fact, the name of the array itself resolves to the address of the 0th element.
Since the elements of an array are placed in adjacent memory locations and the
address of each subscript increments by 4 (in case of an int array), we can use this
feature to traverse the array elements with the help of pointer as well.
Example of a Pointer
The following example shows how you can traverse an array with a pointer −
#include <stdio.h>
int i;
https://www.tutorialspoint.com/cprogramming/c_pointer_vs_array.htm 3/5
6/16/24, 1:14 PM Pointer vs Array in C
return 0;
}
Output
Array Pointer
The number of variables that can be stored is A pointer can store the address of
decided by the size of the array. only a single variable.
The initialization of arrays can be done while Pointers cannot be initialized while
defining them. defining them.
https://www.tutorialspoint.com/cprogramming/c_pointer_vs_array.htm 4/5
6/16/24, 1:14 PM Pointer vs Array in C
https://www.tutorialspoint.com/cprogramming/c_pointer_vs_array.htm 5/5