- C++ Introduction
- C++ Getting Started
- C++ Syntax
- C++ Output (Print Text)
- C++ Comments
- C++ Variables
- C++ Declare Multiple Variables
- C++ Identifiers
- C++ User Input
- C++ Data Types
- C++ Operators
- C++ Strings
- C++ Math
- C++ Booleans
- C++ Conditions
- C++ Switch
- C++ While Loop
- C++ For Loop
- C++ Break And Continue
- C++ Arrays
- C++ References
- C++ Pointers
- C++ Functions
- C++ Function Overloading
- C++ OOP
- C++ Classes And Objects
- C++ Class Methods
- C++ Constructors
- C++ Access Specifiers
- C++ Encapsulation
- C++ Inheritance
- C++ Multilevel Inheritance
- C++ Multiple Inheritance
- C++ Inheritance Access
- C++ Polymorphism
- C++ Files
- C++ Exceptions
- C++ How To Add Two Numbers
C++ Pointers
Creating Pointers
You learned from the previous chapter, that we can get the memory address of a variable by using the &
operator:
Example
cout << food; // Outputs the value of food (Pizza)
cout << &food; // Outputs the memory address of food (0x6dfed4)
A pointer however, is a variable that stores the memory address as its value.
A pointer variable points to a data type (like int
or string
) of the same type, and is created with the *
operator. The address of the variable you're working with is assigned to the pointer:
Example
string* ptr = &food; // A pointer variable, with the name ptr, that stores the address of food
// Output the value of food (Pizza)
cout << food << "\n";
// Output the memory address of food (0x6dfed4)
cout << &food << "\n";
// Output the memory address of food with the pointer (0x6dfed4)
cout << ptr << "\n";
Example explained
Create a pointer variable with the name ptr
, that points to a string
variable, by using the asterisk sign *
(string* ptr
). Note that the type of the pointer has to match the type of the variable you're working with.
Use the &
operator to store the memory address of the variable called food
, and assign it to the pointer.
Now, ptr
holds the value of food
's memory address.
Tip: There are three ways to declare pointer variables, but the first way is preferred:
string *mystring;
string * mystring;
Practice Excercise Practice now
C++ Dereference
Get Memory Address and Value
In the example from the previous page, we used the pointer variable to get the memory address of a variable (used together with the &
reference operator). However, you can also use the pointer to get the value of the variable, by using the *
operator (the dereference operator):
Example
string* ptr = &food; // Pointer declaration
// Reference: Output the memory address of food with the pointer (0x6dfed4)
cout << ptr << "\n";
// Dereference: Output the value of food with the pointer (Pizza)
cout << *ptr << "\n";
Note that the *
sign can be confusing here, as it does two different things in our code:
- When used in declaration (string* ptr), it creates a pointer variable.
- When not used in declaration, it act as a dereference operator.
Practice Excercise Practice now
C++ Modify Pointers
You can also change the pointer's value. But note that this will also change the value of the original variable:
Example
string* ptr = &food;
// Output the value of food (Pizza)
cout << food << "\n";
// Output the memory address of food (0x6dfed4)
cout << &food << "\n";
// Access the memory address of food and output its value (Pizza)
cout << *ptr << "\n";
// Change the value of the pointer
*ptr = "Hamburger";
// Output the new value of the pointer (Hamburger)
cout << *ptr << "\n";
// Output the new value of the food variable (Hamburger)
cout << food << "\n";
Practice Excercise Practice now
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP