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

Kursinhalt

C Basics

C Basics

1. Introduction
2. Data
3. Operators
4. Control Statements
5. Functions
6. Pointers

book
References 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.

c

Main

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

%p is the format specifier used for addresses (pointer).

Addresses are typically expressed in hexadecimal notation.

Think of the & operator as identifying your home's address using your name.

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

Essentially, *&x is the same as x.

c

Main

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

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

Aufgabe

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 (i.e., address + 1).
  • Attempt to dereference the address obtained in the previous step.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 6. Kapitel 2
toggle bottom row

book
References 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.

c

Main

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

%p is the format specifier used for addresses (pointer).

Addresses are typically expressed in hexadecimal notation.

Think of the & operator as identifying your home's address using your name.

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

Essentially, *&x is the same as x.

c

Main

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

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

Aufgabe

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 (i.e., address + 1).
  • Attempt to dereference the address obtained in the previous step.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 6. Kapitel 2
Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
We're sorry to hear that something went wrong. What happened?
some-alt