Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære References and Dereferences | Pointers
C Basics

bookReferences and Dereferences

Pointers are built around two operators:

  • The address-of operator &.
  • The dereference operator *.

Address-of Operator

The address-of operator, represented by &, allows us to directly interact with our computer's RAM. Using & lets you obtain the actual memory address of an object.

Main.c

Main.c

copy
123456789101112
#include <stdio.h> int main() { int x = 100; printf("Value of variable: %d\n", x); printf("Address of variable into RAM: %p\n", &x); // using reference operator `&` for getting address return 0; }
Note
Note

The format specifier used to print addresses (pointers) is %p.

Addresses are typically expressed in hexadecimal notation.

Dereference Operator

Conversely, the * operator gives you the resident's name when given their address. So, how can we employ this operator if we're not directly dealing with addresses? If you have an expression like &x, which returns the address of the x variable, applying the * operator to it (*&x) gives you the value of the variable stored at that address.

Note
Note

Essentially, *&x is the same as x.

Main.c

Main.c

copy
1234567891011121314
#include <stdio.h> int main() { int x = 100; printf("Value of variable: %d\n", x); printf("Address of variable in RAM: %p\n", &x); // using reference operator `&` printf("Dereferencing address of variable: %d", *(&x)); // using dereference operator `*` return 0; }
Note
Note

Don't mix up the dereference operator (*x) with the multiplication operator (x*y).

Oppgave

Swipe to start coding

  • Create an integer array of 5 elements and populate it.
  • Retrieve the address of the third element.
  • Increment the address of the third element and dereference the address obtained.

Løsning

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 6. Kapittel 2
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you explain the difference between the address-of and dereference operators with an example?

What does hexadecimal notation look like in memory addresses?

Can you show how to use these operators in a simple code snippet?

close

Awesome!

Completion rate improved to 2.63

bookReferences and Dereferences

Sveip for å vise menyen

Pointers are built around two operators:

  • The address-of operator &.
  • The dereference operator *.

Address-of Operator

The address-of operator, represented by &, allows us to directly interact with our computer's RAM. Using & lets you obtain the actual memory address of an object.

Main.c

Main.c

copy
123456789101112
#include <stdio.h> int main() { int x = 100; printf("Value of variable: %d\n", x); printf("Address of variable into RAM: %p\n", &x); // using reference operator `&` for getting address return 0; }
Note
Note

The format specifier used to print addresses (pointers) is %p.

Addresses are typically expressed in hexadecimal notation.

Dereference Operator

Conversely, the * operator gives you the resident's name when given their address. So, how can we employ this operator if we're not directly dealing with addresses? If you have an expression like &x, which returns the address of the x variable, applying the * operator to it (*&x) gives you the value of the variable stored at that address.

Note
Note

Essentially, *&x is the same as x.

Main.c

Main.c

copy
1234567891011121314
#include <stdio.h> int main() { int x = 100; printf("Value of variable: %d\n", x); printf("Address of variable in RAM: %p\n", &x); // using reference operator `&` printf("Dereferencing address of variable: %d", *(&x)); // using dereference operator `*` return 0; }
Note
Note

Don't mix up the dereference operator (*x) with the multiplication operator (x*y).

Oppgave

Swipe to start coding

  • Create an integer array of 5 elements and populate it.
  • Retrieve the address of the third element.
  • Increment the address of the third element and dereference the address obtained.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 6. Kapittel 2
single

single

some-alt