Pointer vs Array in C
Delve into the differences between pointers and arrays in C programming. This guide provides a comprehensive overview of arrays as collections of elements stored in contiguous memory locations, and how pointers reference these memory addresses. Learn the distinct advantages and use cases for each, enhancing your understanding of memory management and efficient coding practices in C.
Pointer vs Array in C
Understanding Arrays and Pointers
Arrays and pointers are fundamental constructs in C, closely related but with key differences. While many operations that use pointers can also be done with arrays, they have distinct advantages and use cases. This guide will explain the differences between arrays and pointers in C, their advantages, and how to use them effectively.
Arrays in C
An array in C is a collection of elements of the same type, stored in adjacent memory locations. Arrays are indexed, starting from 0, and are declared using the following syntax:
Syntax: Array Declaration
data_type arr_name [size];
For example, an integer array with 5 elements is declared as:
Example: Array Declaration
int arr[5];
We can also initialize the array with values during its declaration:
Example: Array Initialization
int arr[] = {1, 2, 3, 4, 5};
Example: Traversing an Array
Here's an example that shows how to traverse an array using indexed subscripts:
Example: Traversing an Array
#include
int main () {
/* an array with 5 elements */
int arr[5] = {11, 22, 33, 44, 55};
int i;
/* output each array element's value */
printf("Array values with subscripts: \n");
for(i = 0; i < 5; i++) {
printf("arr[%d]: %d\n", i, arr[i]);
}
return 0;
}
Output
Array values with subscripts:
arr[0]: 11
arr[1]: 22
arr[2]: 33
arr[3]: 44
arr[4]: 55
Pointers in C
A pointer in C is a variable that stores the memory address of another variable. The address-of operator &
returns the address of a variable, and the pointer is declared by prefixing the type with an asterisk *
. For example, the following code declares an integer variable and a pointer to it:
Example: Pointer Declaration
int x = 6;
int *ptr = &x;
In the case of arrays, the address of the 0th element is stored in the pointer:
Example: Pointer to Array
int arr[] = {1, 2, 3, 4, 5};
int *ptr = &arr[0];
Pointer vs Array in C
Both arrays and pointers provide access to memory, but they differ in how they are used and what they represent. The key difference is that an array is a collection of elements of the same type, while a pointer stores the address of a variable or an array. You can use a pointer to traverse array elements:
Example: Traversing an Array with a Pointer
In this example, we traverse the same array using a pointer:
Example: Pointer Traversal
#include
int main () {
/* an array with 5 elements */
int arr[5] = {11, 22, 33, 44, 55};
int *ptr = arr;
int i;
/* output each array element's value */
printf("Array values with pointer: \n");
for(i = 0; i < 5; i++) {
printf("arr[%d]: %d\n", i, *(ptr + i));
}
return 0;
}
Output
Array values with pointer:
arr[0]: 11
arr[1]: 22
arr[2]: 33
arr[3]: 44
arr[4]: 55
Difference Between Arrays and Pointers
The table below highlights the major differences between arrays and pointers:
Array | Pointer |
---|---|
An array stores elements of the same data type in adjacent memory locations. | A pointer stores the address of a variable or an array. |
An array is a collection of elements of similar data types. | A pointer is a variable that holds the address of another variable. |
The size of the array determines the number of elements it can store. | A pointer can store the address of only a single variable at a time. |
Arrays can be initialized when they are declared. | Pointers cannot be initialized when they are declared. |
Arrays are static in nature. | Pointers are dynamic in nature. |
Arrays cannot be resized once their size is declared. | Pointers can point to different memory locations at runtime. |
Array memory allocation happens at compile time. | Pointer memory allocation happens at runtime. |