October 7, 2025
C LanguageProgramming

Exploring the Basics of Pointers in C

Understanding pointer behavior in C through sample code and explanations.

By re4lity

I Started Learning C Language

I began studying C language after participating in a certain event. I’m still learning by trial and error, but here I’ll summarize what I understand so far.

If you have corrections or better expressions, please contact me here.

In this article, I’ll cover the basics of pointers while working through them myself.

What is a Pointer?

A pointer is a variable that stores a memory address (location). While a normal variable holds a “value,” a pointer variable holds the “location where the value is stored.”

For example:

  • Normal variable = an item in a house
  • Pointer = the address of the house

If you know the address, you can go to the house and take out or put in items. That’s how I understand it.

The Advantage of Pointers: Manipulate Variables Across Functions

In C, when you pass a variable to a function, a “copy of the value” is passed by default. So, even if you change the value inside the function, it doesn’t affect the original variable.

However, by using pointers, you can directly manipulate the original variable across functions. This is where pointers shine.

Sample Code

Let’s check with actual code.

#include <stdio.h>

// Receives the address of the variable pointed to by nb from main via pointer
void single_pointer(int *nb)
{
	*nb = 42; // Assign 42 to the value pointed to by the pointer (i.e., n)
}

int main(void)
{
	int *nb; // Pointer variable
	int n;   // Normal variable

	n = 32;     // Initialize n with 32
	nb = &n;    // Store the address of n in pointer variable nb
	
	printf("before : %d\n", *nb); // Check value of n before calling single_pointer
	printf("n address: %p\n", (void *)&n); // Address of variable n
	printf("nb value: %p\n", (void *)nb);  // Value of pointer variable nb (address of n)
	
	single_pointer(nb); // Pass nb (address of n) to the function
	printf("after : %d\n", *nb); // Check value of n after single_pointer
	
	return (0);
}

Example Output

The addresses for n and nb will vary depending on your environment, but the values should be the same.

For reference, my environment is OS: Ubuntu 24.04.3 LTS x86_64, compiler: cc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0, compiled with cc -Wall -Wextra -Werror.

before : 32
n address: 0x7ffeeb8b9a3c
nb value: 0x7ffeeb8b9a3c
after : 42

Detailed Code Explanation

1. Declaring and Initializing Variables

Looking at the main function:

int n;      // Normal variable
int *nb;    // Pointer variable (pointer to int)
n = 32;     // Assign value to n
nb = &n;    // Assign address of n to nb
  • The * in int *nb indicates a “pointer type”
  • &n is the operator to “get the address of variable n”
  • Now, nb “knows” the location of variable n

2. Checking the Value Pointed to by the Pointer

printf("before : %d\n", *nb);  // Output: 32
  • *nb means “the value pointed to by pointer nb” (dereferencing)
  • At this point, it shows the value of n, which is 32

3. Checking Addresses

printf("n address: %p\n", (void *)&n);  // Address of n
printf("nb value: %p\n", (void *)nb);   // Value of nb

Both should display the same address, showing that nb correctly points to n.

4. Manipulating Variables Across Functions

single_pointer(nb);  // Pass address of n

Here, we pass nb (the address of n) to the single_pointer function outside main.

Inside single_pointer:

void single_pointer(int *nb)
{
	*nb = 42;  // Assign 42 to the value pointed to by the pointer
}

*nb = 42 directly changes the value of variable n inside main.

5. Checking the Result

printf("after : %d\n", *nb);  // Output: 42

After the function call, you can see that the value of n has changed from 32 to 42.

Memory Image Diagram

---

Address: 0x7ffeeb8b9a3c
Variable: n
Value: 32 → 42   ← pointed to by nb

---

Address: 0x7ffeeb8b9a40
Variable: nb
Value: 0x7ffeeb8b9a3c (address of n)

---

Pointer Operators

OperatorNameMeaningExample
*Dereference operatorAccess value pointed to by pointer*nb = 42
&Address operatorGet address of variablenb = &n

Why Are Pointers Necessary?

1. Return Multiple Values from a Function

Normally, a function can only return one value with return, but with pointers, you can change multiple values.

void calculate(int a, int b, int *sum, int *product)
{
	*sum = a + b;
	*product = a * b;
}

Here, the function is void, so it doesn’t return anything directly, but sum and product are changed, so two variables are effectively “returned.”

2. Efficiently Handle Large Data

For large data like structs or arrays, you can pass just the address instead of copying the whole data, making processing faster.

3. For Dynamic Memory Allocation

Pointers are necessary when allocating memory dynamically with malloc, etc.

Summary

  • Pointers are variables that store the address (location) of other variables
  • Use & to get the address, and * to access the value pointed to
  • The biggest advantage is being able to directly manipulate variables across functions
  • Essential for efficient memory handling

I sometimes get lost writing this, but basically, think of it as “knowing the address and manipulating the item at that location.” Try running the code and checking how addresses and values change!