What is a pointer in C?

The key idea

A pointer value can refer to an object through its memory address. In a simple declaration such as an integer pointer, the type tells the program what kind of object it is intended to access. The address and the value stored at that address are different things.

The address-of operator obtains an object's address. Dereferencing a valid pointer accesses the object it points to. Neither operation makes an invalid or expired object safe to use.

Follow one object through the example

#include <stdio.h>

int main(void) {
    int score = 7;
    int *p = &score;
    *p = 9;
    printf("%d\n", score);
    return 0;
}

The output is 9. The variable score exists inside main. The pointer p receives its address, and assigning through p updates that same score object. There are not two separate score values in this example.

ExpressionMeaning in this example
scoreThe integer value
&scoreThe address of the score object
pThe pointer value referring to score
*pAccess to the integer through that pointer

The example deliberately avoids printing an address because its numeric representation is not needed to understand the relationship.

A non-null pointer is not automatically safe

A null pointer does not refer to a usable object. However, checking that a pointer is not null is only one part of safety. A pointer may still refer to an object whose lifetime has ended or to storage outside the permitted range.

Before dereferencing, you need a valid object of an appropriate type, a lifetime that still continues and an access that respects the object's bounds. These requirements come from how the pointer was obtained and used.

Understand lifetime before dynamic allocation

A local variable normally stops existing when its block finishes. Returning its address from a function does not extend that lifetime. A later attempt to use that address as though the object still existed is invalid.

Start with pointers to objects whose lifetime is easy to see, as in the example. Dynamic allocation adds ownership and cleanup responsibilities. Learning those responsibilities carefully is more useful than copying allocation calls early.

Arrays and pointers are related, not identical

In many expressions an array is converted to a pointer to its first element. The pointer does not automatically carry the number of elements. A function working through that pointer generally needs a separate length or another clear boundary contract.

This is why knowing an address alone is not enough to process a whole collection safely. Document the length and ensure every index remains inside the valid range.

A practice check

After the example updates score to 9, assign 12 directly to score and then read through p. You should observe 12 because both expressions refer to the same object.

Explain that result before adding a second pointer. Once you can distinguish the pointer value, the pointed-to object and its lifetime, you have a sounder basis for arrays, functions and later data-structure study.

Choose your next step with Gammal Tech

Choose one skill you want to demonstrate, then review the relevant course and its prerequisites. Ask Gammal Tech about the content, study arrangements and current fees before enrolling.

References and course details

Use these references for further study. The examples and exercises in this article are educational; consult the catalogue for course details.

Articles & Guides