#What are pointers and how can I use them?

13 messages · Page 1 of 1 (latest)

alpine raven
#

I need help learning what pointers are and what they do in a simple way because I am a Beginner. All the yt videos i watch, always confuses with something with pointers.

scarlet wedgeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

stoic lotus
#

What do you think a pointer is after those videos? What you dont understand

weary ibex
#

In C every* variable exists somewhere in memory (or RAM*)

Memory basically being able to hold large number of bytes, with each byes being represented by a number representing its "location" called an address

A pointer is basically... just a number. A number representing an address in memory that holds some other data.

Assuming a is an int pointer (i.e. an address)

*a

Uses that number and reads the interger at the address a

Also, assume n is just an int (not a pointer)

&n

Will take the address of where n is located in memory and make a pointer to that address

wise bloom
#

In the simplest terms, a pointer is a type of value that points to a location in memory. A pointer itself doesn't "do" anything per se - rather, developers do things with the memory at the location the pointer refers to. What the developer can do with that pointer varies.

wise bloom
#

To explain the basic usage of a pointer, take this program:

#

;compile -Wall -Wpedantic -Werror -fsanitize=address,undefined -std=c23

#include <stddef.h>
#include <stdio.h>

typedef struct {
    int age;
} Character;

void character_set_age(Character self, int age) {
    self.age = age;
}

int main() {
    Character some_char = {};
    character_set_age(some_char, 32);

    printf("Character age: %d\n", some_char.age);

    return 0;
}
valid dragonBOT
#
Program Output
Character age: 0
wise bloom
#

The result isn't what we expected. We want it to print Character age: 32 - but instead it's zero. Looking at our function signature:

void character_set_age(Character self, int age) {}

It might look like we're taking in an instance of character, and that's what we are doing in a sense. In C, passing an argument by value creates a copy of the argument. So character_set_age isn't operating on some_char. Instead it's operating on a brand new Character value that only exists for the duration of the function. If we want to actually modify the character we've got, we need a way to refer to the memory that holds the original value. This is where pointers come in. Our function should look like this:

void character_set_age(Character *self, int age) {
    self->age = age;
}

Now instead of taking in a Character, we take in a pointer to a region of memory holding a Character. Do note that instead of self.age = ... we use self->age = .... The -> operator allows us to access fields on the pointer as if we were operating on the type. It's a shorthand for what we call dereferencing. The expanded form of this would be:

(*self).age = age;

In a function, prepending * to the start of a pointer variable dereferences it: it accesses the memory at the given pointer and treats it as the type supposedly held by the pointer.

Going back to the main function, we want to somehow give character_set_age() a pointer. We do this via the addressof operator &:

character_set_age(&some_char, 32);

For any given value, prepending & to it will give you the memory address you can use to refer to that region of memory.

#

Putting it all together:

#

;compile -Wall -Wpedantic -Werror -fsanitize=address,undefined -std=c23

#include <stddef.h>
#include <stdio.h>

typedef struct {
    int age;
} Character;

void character_set_age(Character *self, int age) {
    self->age = age;
}

int main() {
    Character some_char = {};
    character_set_age(&some_char, 32);

    printf("Character age: %d\n", some_char.age);

    return 0;
}
valid dragonBOT
#
Program Output
Character age: 32
alpine raven
#

!solved