Search⌘ K

- Examples

Explore various types of C++ pointers such as basic pointers, pointer arithmetic, nullptr usage, function pointers, and pointers to struct members. Understand how pointers interact with data, how to use nullptr safely, and how to manage function calls through pointers, preparing you for advanced pointer and reference concepts in C++.

Basic pointers #

C++
#include <iostream>
int main(){
std::cout << std::endl;
int i{2011};
int* iptr= &i;
std::cout << i << std::endl;
std::cout << "iptr: " << iptr << std::endl;
std::cout << "*iptr: " << *iptr << std::endl;
std::cout << std::endl;
int * jptr = iptr;
*jptr = 2014;
std::cout << "iptr: " << iptr << std::endl;
std::cout << "*iptr: " << *iptr << std::endl;
std::cout << "jptr: " << jptr << std::endl;
std::cout << "*jptr: " << *jptr << std::endl;
}

Explanation #

  • This example shows an instance of two pointers pointing to the same object.

  • Since both iptr and jptr point to i, changing the dereferenced value of jptr in line 18 changes the values of i and iptr as well. ...

Pointer arithmetic