Pointers and Arrays in C
Pointers and Arrays in C
Arrays in C
An array in C is a homogenous collection of elements of a single data type stored in
a continuous block of memory. The size of an array is an integer inside square
brackets, put in front of the name of the array.
Declaring an Array
To declare an array, the following syntax is used −
data_type arr_name[size];
Each element in the array is identified by a unique incrementing index, starting from
"0". An array can be declared and initialized in different ways.
You can declare an array and then initialize it later in the code, as and when
required. For example −
int arr[5];
...
...
a[0] = 1;
a[1] = 2;
...
...
You can also declare and initialize an array at the same time. The values to be stored
are put as a comma separated list inside curly brackets.
Note: When an array is initialized at the time of declaration, mentioning its size is
optional, as the compiler automatically computes the size. Hence, the following
https://www.tutorialspoint.com/cprogramming/c_pointers_and_arrays.htm 1/6
6/16/24, 12:50 PM Pointers and Arrays in C
All the elements in an array have a positional index, starting from "0". The lower
bound of the array is always "0", whereas the upper bound is "size − 1". We can
use this property to traverse, assign, or read inputs into the array subscripts with a
loop.
#include <stdio.h>
int main(){
int a[5], i;
return 0;
}
Output
a[0] = 712952649
a[1] = 32765
a[2] = 100
a[3] = 0
a[4] = 4096
Pointers in C
https://www.tutorialspoint.com/cprogramming/c_pointers_and_arrays.htm 2/6
6/16/24, 12:50 PM Pointers and Arrays in C
A pointer is a variable that stores the address of another variable. In C, the symbol
(&) is used as the address-of operator. The value returned by this operator is
assigned to a pointer.
To declare a variable as a pointer, you need to put an asterisk (*) before the name.
Also, the type of pointer variable must be the same as the type of the variable whose
address it stores.
In this code snippet, "b" is an integer pointer that stores the address of an integer
variable "a" −
int a = 5;
int *b = &a;
In case of an array, you can assign the address of its 0th element to the pointer.
In C, the name of the array itself resolves to the address of its 0th element. It
means, in the above case, we can use "arr" as equivalent to "&[0]":
int *b = arr;
Unlike a normal numeric variable (where the increment operator "++" increments its
value by 1), the increment operator used with a pointer increases its value by the
sizeof its data type.
#include <stdio.h>
int main(){
int a = 5;
int *b = &a;
https://www.tutorialspoint.com/cprogramming/c_pointers_and_arrays.htm 3/6
6/16/24, 12:50 PM Pointers and Arrays in C
return 0;
}
Output
Address of a: 6422036
After increment, Address of a: 6422040
Hence, the following statement returns "5", which is the value stored in the variable
"a", the variable that "b" points to.
int a = 5;
int *b = &a;
printf("value of a: %d\n", *b);
We can use this property of the pointer to traverse the array element with the help
of a pointer.
#include <stdio.h>
int main(){
b++;
https://www.tutorialspoint.com/cprogramming/c_pointers_and_arrays.htm 4/6
6/16/24, 12:50 PM Pointers and Arrays in C
b++;
printf("Address of a[2]: %d value at a[2] : %d\n", b, *b);
b++;
printf("Address of a[3]: %d value at a[3] : %d\n", b, *b);
b++;
printf("Address of a[4]: %d value at a[4] : %d\n", b, *b);
return 0;
}
Output
Points to Note
It may be noted that −
https://www.tutorialspoint.com/cprogramming/c_pointers_and_arrays.htm 5/6
6/16/24, 12:50 PM Pointers and Arrays in C
We can use this property and use a loop to traverse the array with the dereference
operator.
#include <stdio.h>
int main(){
return 0;
}
Output
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5
You can also increment the pointer in every iteration and obtain the same result −
The concept of arrays and pointers in C has a close relationship. You can use pointers
to enhance the efficiency of a program, as pointers deal directly with the memory
addresses. Pointers can also be used to handle multi−dimensional arrays.
https://www.tutorialspoint.com/cprogramming/c_pointers_and_arrays.htm 6/6