DEV Community

Cover image for Understanding Pointers in C — From a Beginner’s Brain
Krish Jaiswal
Krish Jaiswal

Posted on

Understanding Pointers in C — From a Beginner’s Brain

_⚠️ Disclaimer: I'm still learning C and this post is part of my journey into systems programming. I'm sharing what I've understood so far — if you spot a mistake or want to add something, feel free to comment! I'm here to learn. 😊

So, I recently started learning about pointers in C — and let me tell you, it's like discovering that variables have a secret superpower: they can point to the memory itself! Its definitely complex but also kind of cool.

Here's what I’ve learned so far:

What Are Pointers?

As the name suggests, a pointer is a variable that "points" to another variable — more specifically, it stores the memory address of that variable. Instead of working with the value directly, we work with its address.

Syntax:

int a = 5;
int *ptr = &a;
Enter fullscreen mode Exit fullscreen mode

*ptr means we're declaring a pointer to an int.

&a gives us the memory address of a.

ptr now holds the address of a, not its value.

Why Use Pointers?

Pros

  • Pass by reference: You can pass the address of a variable to a function, which means the original value can be modified — no need to copy large data like structs.
  • Efficient memory use: Avoids the performance overhead of copying large data.

Cons

  • Can make code harder to read, especially when using multiple levels of pointers like *ptr or even **ptr (brain explosion alert 💥)
  • Misusing them can lead to bugs like segmentation faults, memory leaks, and pointer arithmetic disasters.

Pointer Size

I learned that the size of a pointer is the same regardless of what it points to. On a 64-bit system, it's usually 8 bytes. On 32-bit systems, it’s typically 4 bytes. Why? Because it’s just an address — not the actual data.

Void Pointers

A void* (void pointer) is like a generic pointer — it can point to any data type, but you need to cast it to the right type before dereferencing.

Example:

int a = 5;
void *ptr = &a;
printf("%d\n", *(int *)ptr); // Cast back to int* before dereferencing
Enter fullscreen mode Exit fullscreen mode

output :

5 
Enter fullscreen mode Exit fullscreen mode

Pretty cool, right?

Pointers Arithmetic - Math with memory

One of the wildest things I learned is that you can actually do math with pointers. Like, literally add or subtract numbers from them , but it's not just math , it's type-aware math.

Here's what that means:

int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr;
printf("%d\n", *(ptr + 2)); // prints 30
Enter fullscreen mode Exit fullscreen mode

output :

30
Enter fullscreen mode Exit fullscreen mode

You might think ptr + 2 means "move ahead by 2 bytes" - but nope. Since ptr is an int*, and each int is 4 bytes (on most systems), its actually moves * bytes forward to the third element (30).

so:

  • ptr + 1 ---> next integer
  • ptr + 2 ---> two integers ahead
  • ptr - 3 ---> one integer back ( if you're not at the start)

That's how pointer arithmetic work . Its clean, powerfull, and a little dangerous if misused.

pointer airthmetic depends on the dat type size, not raw bytes.

we can also use ptr++ or ptr-- as we do in programing.

Arrays and Pointers — The Similarity

One thing that surprised me is how arrays and pointers are deeply related. As from pointers arithmetic you must got the idea of how similar it works as an array.

example :

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

int main(){

  //array
  int arr[5];
  arr[0]=42;
  arr[1]=1332;
  arr[2]=-21;
  for (int i = 0; i < 5; i++) {
    printf("%d\n",arr[i]);

  }

  printf("\n\n");

  //fake array
  int *ptr = malloc(sizeof(int)*5);
  *(ptr + 0) = 42;
  *(ptr + 1) = 1332;
  *(ptr + 2) = -21;
  for (int i = 0; i < 5; i++) {
    printf("%d\n", *(ptr + i));

  }
return 0;
}
Enter fullscreen mode Exit fullscreen mode

output :

42
1332
-21
32608
37


42
1332
-21
0
0
Enter fullscreen mode Exit fullscreen mode

Both arr[i] and *(ptr + i) give the same result for the values which we declared — because arr[i] is basically syntactic sugar for *(arr + i).
For the last 2 values in both array and pointers are the garbage value since we didn't declared them . They might also be different in your case.
So, Pointer arithmetic win!
note : (Don’t forget to #include and free(ptr) when using malloc.)

Still Confused? Me Too... A Bit

I’ll be honest — I don’t fully understand pointers yet. But writing this helped me get a lot clearer. Maybe someday I’ll read this again and laugh at my beginner confusion (or maybe cry 😂).

But for now — this is where I’m at.
If you’re also learning, drop a comment! Let’s get confused together and grow past it. 💪

TL;DR

  • Pointers stores addresses, not actual values.
  • Use * to declare and dereference.
  • They're useful for passing by reference, reducing memory overhead.
  • Void Pointers are generic but need casting.
  • We can also do pointer arithmetic.
  • Pointers and array are tightly linked.
  • You can do pointer arithmetic ( like ptr + 2 ) to move across memory .
  • It's confusing at first, but once it clicks - its chef's kiss.

Thanks for reading! 🙌
If you liked this or have feedback, leave a reply. More beginner blogs coming soon as I continue my dev journey!

Resources I Used
freeCodeCamp youtube video on pointers : https://youtu.be/MIL2BK02X8A
its taught by a man with youtube channel www.youtube.com/@onaecO he gave excellent explanation and examples . You can also check out his channel for more cool programming videos.

Top comments (0)