pointer in c++
// Variable is used to store value
int a = 5;
cout << a; //output is 5
// Pointer is used to store address of variable
int a = 5;
int *ab;
ab = &a; //& is used get address of the variable
cout << ab; // Output is address of variable
pointer in c++
// Variable is used to store value
int a = 5;
cout << a; //output is 5
// Pointer is used to store address of variable
int a = 5;
int *ab;
ab = &a; //& is used get address of the variable
cout << ab; // Output is address of variable
c++ pointer
#include <iostream>
void pointers_with_dynamic_memory() {
int arr_size; // arr_size can contain an integer
int* x; // x can contain the memory address of an integer.
std::cout << "enter an integer > 0: ";
std::cin >> arr_size;
// value of x is the starting address of a heap-allocated block ints
x = new int[arr_size];
x[0] = 10; // set first element of array to 10
*x = 50; // set first element of array to 50
*(x + 1) = 100; // +1 offsets automatically by the value of sizeof(int)
delete [] x;
}
pointer in c++
int* pointVar, var;
var = 5;
// assign address of var to pointVar
pointVar = &var;
// access value pointed by pointVar
cout << *pointVar << endl; // Output: 5
In the above code, the address of var is assigned to the pointVar pointer.
We have used the *pointVar to get the value stored in that address.
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us