🦋Pointers
Last updated
Was this helpful?
Was this helpful?
int main()
{
int x = 10;
std::cout << "location of x: " << &x << std::endl;
}int main()
{
int x = 10;
std::cout << typeid(x).name() << std::endl;
std::cout << typeid(&x).name() << std::endl;
}int main()
{
int x = 10;
int* p = &x;
}int main()
{
int x = 10;
int* p = &x;
// can i get 10 using variable p alone?
}int main()
{
int x = 10;
int* p = &x;
int y = *p;
std::cout << "value of x: " << x << std::endl;
std::cout << "value of y: " << y << std::endl;
}#include <iostream>
int main() {
int x = 10;
int* y = &x; // y points to x
int* z = &x; // z also points to x
std::cout << x << std::endl; // 10
std::cout << *y << std::endl; // 10
*y = 20; // modify via pointer
std::cout << x << std::endl; // 20 (x changed!)
}#include <iostream>
int main() {
int x = 10; // normal variable
int* p = &x; // pointer to x
int** pp = &p; // pointer to pointer
std::cout << "x value: " << x << std::endl;
std::cout << "Address of x (&x): " << &x << std::endl;
std::cout << "p (points to x): " << p << std::endl;
std::cout << "Value at p (*p): " << *p << std::endl;
std::cout << "pp (points to p): " << pp << std::endl;
std::cout << "Value at pp (*pp): " << *pp << std::endl;
std::cout << "Value at *pp (**pp): " << **pp << std::endl;
return 0;
}