Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Introduction to Pointer | Pointers Fundamentals
C++ Pointers and References
course content

Kursinnehåll

C++ Pointers and References

C++ Pointers and References

1. Pointers Fundamentals
2. Pointer Arithmetic
3. References Fundamentals
4. Dynamic Memory Allocation

book
Introduction to Pointer

A pointer is a variable that stores the memory address of another variable. It allows direct manipulation of memory. Declaration and initialization of a pointer looks like this:

h

pointer

copy
1
int* p_Name = nullptr;
  • int: specifies the base type of the variable that the pointer will point to. In this case, it's an integer;

  • *: called dereference operator, returns the value stored at the address held by a pointer;

  • p_Name: the name of the pointer variable. You can choose any valid variable name;

  • nullptr: is a keyword that represents a null pointer, indicating that it is empty and doesn't point to anything.

Note

Commonly, pointers are named with a prefix p_ as a naming convention signifying that the variable is a pointer.

Uppgift

Swipe to start coding

  1. Declare and initialize pointer with a nullptr.
  2. Display a pointer for a chosen primitive data type.
  3. Make sure its name has p_ prefix.

Lösning

cpp

solution

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 1
toggle bottom row

book
Introduction to Pointer

A pointer is a variable that stores the memory address of another variable. It allows direct manipulation of memory. Declaration and initialization of a pointer looks like this:

h

pointer

copy
1
int* p_Name = nullptr;
  • int: specifies the base type of the variable that the pointer will point to. In this case, it's an integer;

  • *: called dereference operator, returns the value stored at the address held by a pointer;

  • p_Name: the name of the pointer variable. You can choose any valid variable name;

  • nullptr: is a keyword that represents a null pointer, indicating that it is empty and doesn't point to anything.

Note

Commonly, pointers are named with a prefix p_ as a naming convention signifying that the variable is a pointer.

Uppgift

Swipe to start coding

  1. Declare and initialize pointer with a nullptr.
  2. Display a pointer for a chosen primitive data type.
  3. Make sure its name has p_ prefix.

Lösning

cpp

solution

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 1
Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Vi beklagar att något gick fel. Vad hände?
some-alt