Page Content

Tutorials

What Is The Relationship Between Pointers And Arrays In C?

Explore the Relationship Between Pointers And Arrays In C: how array names often act as pointers, pointer arithmetic for array access, and their interchangeable use in many contexts.

Pointers and One-Dimensional Arrays

In C, pointers and one-dimensional arrays do in fact have a close and essential link. One of C’s most potent features is its pointers.

First, let’s review some fundamental pointer ideas:

  • A variable that holds the memory address of another variable is called a pointer. Indirect access to data is made possible by this.
  • A unary operator called the address operator (&) yields a variable’s memory address. For instance, &x provides the variable x’s address.
  • A unary operator that accesses the value stored at the memory address that a pointer points to is the indirection operator (*), also known as the dereferencing operator. *ip denotes the value of x if ip points to it. Dereferencing is the process of using * to retrieve the value that a pointer points to.

Example: For dereferencing, obtaining an address, and declaring a pointer:

int x = 1;
int *ip; /* ip is a pointer to int */ 
ip = &x; /* ip is now points to x */ 
// Accessing the value of x using the pointer ip (dereferencing)
int y = *ip; // y will be 1 
// Modifying the value of x using the pointer ip
*ip = 10; // x will now be 10 

Output:

Value of y: 1
Value of x after modification: 10

This demonstrates how to use the * operator to read or change the data at that address and how to store the address that was obtained by the & operator in a pointer variable (ip).

Relationship Between Pointers And Arrays in C

Contiguous sets of memory regions containing identically typed data items are called arrays. In C, arrays and pointers are closely linked and frequently used interchangeably.

One important idea is that an array name can be viewed as a constant pointer to the array’s first member when it is used without subscripts. The address of the first element is represented by the array name itself. The address of the first element (a) of a one-dimensional array a can be written as either &a or just a. Therefore, either vPtr = &values; or vPtr = values; can be used to assign the beginning address of an array values to a pointer variable vPtr.

Pointer Arithmetic and Accessing Array Elements

Certain arithmetic operations on pointers, such as incrementing (++), decrementing (–), adding, and subtracting integers, are permitted in C. A pointer that points to an element in an array increases by i times the size of the object it points to when an integer i is added to it. The element I places further away in the array is automatically pointed to by this. A logic error may occur when pointer arithmetic is performed on pointers that do not relate to array elements (or one past the last element). Pointers that point to the same elements in the same array are the most significant to compare. The number of items between two pointers can be obtained by subtracting one from the other.

Because of this link, pointer notation which is frequently used interchangeably with array subscript notation allows array elements to be accessed. The equation a[i] is equal to *(a + i) for an array a and an integer i.

Pointers can be used in a variety of ways to access array elements:

  • Array Subscript Notation (arrayName[subscript]): The most popular and straightforward method is arraySubscript Notation (arrayName[subscript]). For instance, b[i].
  • Pointer/Offset Notation (*(pointer + offset)): Using the array name or a pointer variable as the base address and adding an integer offset is known as pointer/offset notation (*(pointer + offset)). *(b + offset) or *(bPtr + offset) are two examples.
  • Pointer Subscript Notation (pointer[subscript]): Pointers can be subscripted like arrays using the Pointer Subscript Notation (pointer[subscript]). For instance, bPtr[i].

An Example of Code Showing the Pointer-Array Relationship

// fig07_14.cpp
// Using subscripting and pointer notations with arrays. 
#include <stdio.h> // Include standard input/output library 
#define ARRAY_SIZE 4 // Define a symbolic constant for array size 
int main(void) { // Main function definition 
   int b[] = {10, 20, 30, 40}; // create and initialize array b 
   int *bPtr = b; // create bPtr and point it to array b 
                   // The array name 'b' is treated as a pointer to the first element 
   // output array b using array subscript notation 
   puts("Array b printed with:\nArray subscript notation"); // Print header
   // loop through array b 
   for (size_t i = 0; i < ARRAY_SIZE; ++i) { // Loop through elements
      printf("b[%zu] = %d\n", i, b[i]); // Access elements using array notation 
   } // End for loop 
   // output array b using array name and pointer/offset notation
   puts("\nPointer/offset notation where the pointer is the array name"); // Print header
   // loop through array b 
   for (size_t offset = 0; offset < ARRAY_SIZE; ++offset) { // Loop using offset
      printf("*(b + %zu) = %d\n", offset, *(b + offset)); // Access using pointer/offset notation with array name
      // *(b + offset) is equivalent to b[offset]
   } // End for loop 
   // output array b using bPtr and array subscript notation 
   puts("\nPointer subscript notation"); // Print header 
   // loop through array b 
   for (size_t i = 0; i < ARRAY_SIZE; ++i) { // Loop through elements 
      printf("bPtr[%zu] = %d\n", i, bPtr[i]); // Access using pointer subscript notation 
      // bPtr[i] is equivalent to *(bPtr + i)
   } // End for loop 
   // output array b using bPtr and pointer/offset notation 
   puts("\nPointer/offset notation"); // Print header
   // loop through array b 
   for (size_t offset = 0; offset < ARRAY_SIZE; ++offset) { // Loop using offset 
      printf("*(bPtr + %zu) = %d\n", offset, *(bPtr + offset)); // Access using pointer/offset notation with pointer variable 
   } // End for loop 
} // End main function 

Output:

Array b printed with:
Array subscript notation
b[0] = 10
b[1] = 20
b[2] = 30
b[3] = 40
Pointer/offset notation where the pointer is the array name
*(b + 0) = 10
*(b + 1) = 20
*(b + 2) = 30
*(b + 3) = 40
Pointer subscript notation
bPtr[0] = 10
bPtr[1] = 20
bPtr[2] = 30
bPtr[3] = 40
Pointer/offset notation
*(bPtr + 0) = 10
*(bPtr + 1) = 20
*(bPtr + 2) = 30
*(bPtr + 3) = 40

In conclusion, flexible memory access in C is made possible by the intimate connection between pointers and one-dimensional arrays. Array elements can be accessed via the well-known array subscript notation or different pointer notations requiring offsets and dereferencing, frequently with comparable outcomes. An array name acts as a pointer to the array’s beginning. When paired with pointer arithmetic, this flexibility makes C programming extremely potent. Strings, which are character arrays accessed by pointers, are likewise covered by this relationship.

Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.
Index