📚 C Programming Tutorial

C Programming

Learn C programming concepts with simple explanations, examples and practical notes.

← Back to C Programming

Arrays to Pointers

Arrays and pointers in C

Arrays and pointers are closely related concepts in C programming. Understanding their relationship helps you write efficient programs, pass arrays to functions, process strings, and work with memory addresses.

These notes cover the definition, relationship, pointer arithmetic, accessing array elements using pointers, examples with outputs, common mistakes, and important B.Tech exam questions.

Relationship between arrays and pointers

An array stores multiple elements of the same data type in contiguous memory locations. A pointer is a variable that stores a memory address.

Consider this declaration:

int a[5] = {10, 20, 30, 40, 50};

The array contains five integer elements, indexed from 0 to 4.

In most expressions, the array name a is converted to a pointer to its first element. Therefore:

a == &a[0]

This means the array name can be used to access elements through pointer operations. Assuming each int occupies 4 bytes, the next element is 4 bytes after the previous one.

For an array a, the following expressions are equivalent in value in most ordinary expressions:

Expression Meaning
a Converts to a pointer to the first element in most expressions
&a[0] Address of the first element
*a Value of the first element
a[0] Value of the first element
a + 1 Pointer to the second element
*(a + 1) Value of the second element

Important: An array and a pointer are not the same thing. The array name is not a modifiable pointer variable, and the array itself is not a pointer object. It is converted to a pointer in most expressions, with some exceptions such as sizeof(a) and &a.

Declaring and initializing a pointer to an array element

A pointer can store the address of an array element.

Syntax:

data_type *pointer_name;

Example:

int a[5] = {10, 20, 30, 40, 50};
int *p;

p = &a[0];

Here:
- a is an integer array.
- p is a pointer to an integer.
- &a[0] gives the address of the first element.
- p stores that address.

You can also write:

int *p = a;

This initializes p with a pointer to a[0].

Accessing array elements using pointers

There are two common ways to access array elements: using array notation and using pointer notation.

a[0]     // First element
*a       // First element

a[1]     // Second element
*(a + 1) // Second element

a[2]     // Third element
*(a + 2) // Third element

The general relationship is:

a[i] = *(a+i)

This works because pointer arithmetic advances by elements, not by individual bytes.

Example: C Program to access array elements using a pointer

#include <stdio.h>

int main()
{
    int a[5] = {10, 20, 30, 40, 50};
    int *p = a;
    int i;

    for (i = 0; i < 5; i++)
    {
        printf("%d ", *(p + i));
    }

    return 0;
}

Output:

10 20 30 40 50

Explanation

  1. int *p = a; makes p point to the first element.
  2. p + i points to the element at index i.
  3. *(p + i) accesses the value at that address.
  4. The loop displays every element.

Pointer arithmetic with arrays

Pointer arithmetic allows us to move between array elements.

Suppose:

int a[4] = {5, 10, 15, 20};
int *p = a;

Here p points to a[0], which holds 5. Then a[1] holds 10, a[2] holds 15, and a[3] holds 20. For the expression p + 0, the value *(p + 0) is 5, because pointer arithmetic moves by 0 elements from the first element.

Common pointer arithmetic operations:

Expression Meaning
p++ Move to the next element
p-- Move to the previous element
p + i Point to the element i positions ahead
*(p + i) Access the element i positions ahead
p2 - p1 Number of elements between two pointers into the same array, or one past it

For example, if p points to an int and sizeof(int) is 4 bytes, p + 1 advances by 4 bytes. The actual byte distance depends on the type and implementation.

Pointer arithmetic must stay within the same array object or one position past its end. A one-past-the-end pointer may be formed, but it must not be dereferenced.

Modifying array elements using pointers

Pointers can also be used to modify the original array elements.

Example: C Program to modify an array element using a pointer

#include <stdio.h>

int main()
{
    int a[5] = {10, 20, 30, 40, 50};
    int *p = a;

    *(p + 2) = 100;

    printf("Third element = %d\n", a[2]);

    return 0;
}

Output:

Third element = 100

Explanation

  • p points to a[0].
  • p + 2 points to a[2].
  • *(p + 2) = 100; changes the third element.
  • Since the pointer refers to the original array, a[2] now contains 100.

Traversing an array using a pointer

An array can be traversed using a pointer and a loop.

#include <stdio.h>

int main()
{
    int a[5] = {2, 4, 6, 8, 10};
    int *p = a;
    int i;

    for (i = 0; i < 5; i++)
    {
        printf("%d ", *p);
        p++;
    }

    return 0;
}

Output:

2 4 6 8 10

Here, p++ advances the pointer to the next array element during each iteration.

Notice that this program modifies the pointer variable p, not the array name a. The array name cannot be incremented like a pointer variable.

Difference between an array and a pointer

Although arrays and pointers are closely related, they are different in C.

Feature Array Pointer
Example declaration int a[5]; int *p;
What it is An array object containing elements A variable that can store an address
Memory Space for its elements is allocated Space for the pointer object itself
Assignment Cannot assign a new array to an existing array Can assign another compatible address
Increment a++ is invalid p++ is allowed when valid
sizeof Gives the size of the entire array when applied directly to it Gives the size of the pointer object
Function parameter Array parameter declaration adjusts to a pointer parameter Remains a pointer parameter

Example: Difference in sizeof

#include <stdio.h>

int main()
{
    int a[5] = {1, 2, 3, 4, 5};
    int *p = a;

    printf("Array size = %zu\n", sizeof(a));
    printf("Pointer size = %zu\n", sizeof(p));

    return 0;
}

On a typical system with 4-byte integers and 8-byte pointers, the output is:

Array size = 20
Pointer size = 8

These sizes are implementation-dependent. sizeof(a) gives the total size of the five integer elements, while sizeof(p) gives the size of the pointer itself.

Passing an array to a function using a pointer

An array can be processed using a pointer parameter.

#include <stdio.h>

int sumArray(const int *p, int size)
{
    int sum = 0;
    int i;

    for (i = 0; i < size; i++)
    {
        sum += *(p + i);
    }

    return sum;
}

int main()
{
    int a[4] = {10, 20, 30, 40};

    printf("Sum = %d\n", sumArray(a, 4));

    return 0;
}

Output:

Sum = 100

In this example:
- a converts to a pointer to its first element when passed to sumArray.
- const int *p means the function cannot modify the elements through p.
- size tells the function how many elements it may process.
- *(p + i) accesses each element.

Important: An array's length is not automatically passed to a function. Pass the length separately or use another suitable way to determine it.

Important rules to remember

  1. In most expressions, an array name converts to a pointer to its first element.
  2. a[i] is equivalent to *(a + i).
  3. &a[i] gives the address of element i.
  4. *p accesses the element pointed to by p.
  5. p++ advances to the next element of the pointer's type.
  6. The array name cannot be assigned a new address or incremented.
  7. A pointer must be initialized to a valid address before it is dereferenced.
  8. Do not dereference a one-past-the-end pointer or access elements beyond the array bounds.

Practice questions

  1. Explain the relationship between arrays and pointers in C with suitable examples.
  2. Explain pointer arithmetic and demonstrate how to access array elements using pointers.
  3. Differentiate between arrays and pointers.
  4. Write a C program to traverse an array using pointers.
  5. Explain how an array can be passed to a function using a pointer parameter.