DEV Community

Charan Gutti
Charan Gutti

Posted on

Simple guide to pointers and struct pointers in c

Many people including me have been confused with the concept of understanding what is a pointer. Lets learn it the simple way

Before learning the pointers lets do a conceptual/story type understanding of this.

Why should you use pointers ?

Scenario 1: Pass by Value (The Inefficient Way)
If you write the function like this:

void check_health(struct PlayerProfile player)
Enter fullscreen mode Exit fullscreen mode

every time you call it, the computer has to copy the entire structure. It's like photocopying a 200-page book every time you want to show someone a single sentence. This wastes memory and, more importantly, takes time.

Scenario 2: Pass by Pointer (The Efficient Way)
If you write the function with a pointer:

void check_health(struct PlayerProfile* player)
Enter fullscreen mode Exit fullscreen mode

you are not passing the book; you are just passing a small sticky note with the book's location in the library (its memory address). A memory address is tiny (usually 4 or 8 bytes), regardless of how big the data structure is. The function can then use that address to go look at the original data.


A Simple Story

  1. Imagine the system memory to be a city 🏙️

  2. Now this city will have multiple buildings 🏢

  3. Each building will have a location📍


Combining the knowledge with the concept.

  1. memory - city 🏙️

  2. each building - main object 🏢

  3. location - address 📍

lets understand and simplify this with a practical example

int object = 20
int* pointer = &object

Enter fullscreen mode Exit fullscreen mode

&object will give the address of the object
pointer will now have the address of the variable

So if you call pointer you will have the address of the object.
code :

printf(pointer)
Enter fullscreen mode Exit fullscreen mode

O/P :

0x7fffe8f5591c
Enter fullscreen mode Exit fullscreen mode

Most of the times you want to have the value of the object thats where the de-reference variable (*) comes into play.

code:

printf(*pointer)
Enter fullscreen mode Exit fullscreen mode

O/P:

20
Enter fullscreen mode Exit fullscreen mode

Lets move onto struct pointers

struct Player {
int score;
int health
}

//initializing struct
struct Player* player_ptr;

//Allocating memory
player_ptr = (struct Player*) malloc(sizeof(struct Player));

//Values
printf(*player_ptr.score);
printf(*player_ptr.health);

// Alternative method
printf(player_ptr->score);
printf(player_ptr->health);
Enter fullscreen mode Exit fullscreen mode

Can you do this without allocating memory?

Yes, you absolutely can. You can achieve this by creating the struct variable directly (which allocates memory on the stack automatically) and then making a pointer that points to it.

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

// Define the struct
struct Player {
    int score;
    int health;
};

int main() {
    // 1. Declare a struct variable directly.
    // Memory is now automatically allocated on the stack for it.
    struct Player my_player;

    // 2. Create a pointer that points to the existing variable.
    struct Player* player_ptr = &my_player;

    // 3. Assign values using the pointer.
    player_ptr->score = 500;
    player_ptr->health = 75;

    // Access the values to prove it works
    printf("Player Score: %d\n", player_ptr->score);
    printf("Player Health: %d\n", player_ptr->health);

    // Notice: NO free() call is needed because we did not use malloc().
    // The memory for 'my_player' is automatically cleaned up when main() ends.

    return 0;
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)