HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to CVariablesData TypesOperatorsStringsArraysConditions (if / switch)Loops (for / while)FunctionsPointers
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🔁 Condition & Loop
🧩 Function
👉 Pointer
C›Pointers

Pointers

Accessing memory addresses with & (address) and * (value) — C's most powerful feature.

Simple enough for a 5-year-old to understand

& and * basics

& gives the ADDRESS of a variable; * accesses the VALUE at that address. A pointer is a variable that holds an address.

Example code
1
#include <stdio.h>
2
int main() {
3
int x = 10;
Normal variable.
VariableNormal variable.
4
int *p = &x;
p holds the address of x.
Variablep holds the address of x.
5
printf("%d\n", *p);
*p = value of x: 10
Output / Input*p = value of x: 10
6
*p = 20;
Modify the value at the address.
CodeModify the value at the address.
7
printf("%d\n", x);
x is now 20!
Output / Inputx is now 20!
8
return 0;
9
}
VariableOutput / InputCode
💡 Hint: & means "address", * means "value at that address". When you write *p = 20, x also changes because p points to x.

About this topic

A pointer is a variable that holds the memory address of a value rather than the value itself. This is what sets C apart from other languages: direct memory access. Pointers are known to be a scary topic, but they are actually based on a single idea — an address is also a value and can be stored in a variable.

Two main operations are performed with pointers: getting the address of a variable and accessing the value at an address. This second operation is called dereferencing and also allows you to write to the location pointed to by the pointer. This is how functions can modify the caller's variable.

The danger of pointers arises when they do not point to a valid location. Reading a pointer that points to nowhere crashes the program; worse is a pointer pointing to freed memory because the program doesn't crash, it continues running with incorrect data.

What you will learn

  • The address-of and dereferencing operations
  • Why pointers carry type information
  • Why NULL checking is mandatory
  • Allocating memory with malloc and freeing it with free
  • The relationship between arrays and pointers

Who is this for?

  • Those taking a data structures course and needing to write a linked list
  • Those who want to learn dynamic memory management
  • Those who want to understand the interfaces of C libraries

When to use?

A pointer is used when a function needs to modify the caller's variable; there is no other way to do this in C.

Memory is allocated with malloc for data whose size is determined at runtime. Every allocated block must be freed with free, otherwise memory consumption increases as the program runs.

Passing the address of a large struct to a function instead of copying it is both fast and reduces stack usage.

Common mistakes

Wrong

#include <stdio.h>

int main() {
    int *p;
    *p = 10;
    printf("%d\n", *p);
    return 0;
}

p was not set to anywhere; it contains a random address, and writing to it will crash the program or overwrite other data. This error is called a wild pointer. As soon as a pointer is defined, it must be set either to a valid address or to NULL.

Correct

#include <stdio.h>

int main() {
    int value = 10;
    int *p = &value;
    printf("%d\n", *p);
    return 0;
}

Wrong

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *arr = malloc(5 * sizeof(int));
    arr[0] = 42;
    printf("%d\n", arr[0]);
    return 0;
}

There are two missing things. If malloc fails, it returns NULL, and since it's not checked, the first write will cause a crash. Also, the allocated memory was not freed with free; this seems trivial in this small program, but in code running inside a loop, it creates a memory leak.

Correct

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *arr = malloc(5 * sizeof(int));
    if (arr == NULL) {
        return 1;
    }
    arr[0] = 42;
    printf("%d\n", arr[0]);
    free(arr);
    return 0;
}

Wrong

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *p = malloc(sizeof(int));
    *p = 7;
    free(p);
    printf("%d\n", *p);
    return 0;
}

After free is called, that memory is no longer yours; reading it is undefined behavior. The program usually doesn't crash and prints the old value, which is why the bug goes unnoticed. This is called a dangling pointer. Setting the pointer to NULL after free makes this bug visible.

Correct

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *p = malloc(sizeof(int));
    *p = 7;
    printf("%d\n", *p);
    free(p);
    p = NULL;
    return 0;
}

Frequently asked questions

Why does a pointer carry type information, isn't an address just a number after all?

The type is necessary for two things: determining how many bytes to read from the address during dereferencing, and knowing how large the step will be in pointer arithmetic. Incrementing an integer pointer moves the address forward by four bytes, not one.

Are arrays and pointers the same thing?

No, but they are close relatives. An array name decays to the address of the first element in most contexts, which is why they behave similarly. The difference is: an array cannot be reassigned, but a pointer can; furthermore, sizeof gives the total size for an array, and the address size for a pointer.

How do I keep track of balancing malloc and free?

Adopt the rule that the code which allocates memory is also responsible for freeing it, and keep allocation and freeing in the same logical layer. In practice, tools like Valgrind or address sanitizers report unfreed blocks along with their line numbers.

Summary

A pointer holds the address of a value, not the value itself.

The ampersand takes the address, the asterisk accesses the value at the address.

Every pointer must be set either to a valid address or to NULL; NULL checking must not be skipped.

Every block allocated with malloc is freed with free, then the pointer is set to NULL.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code