Laurand Osmeni

Let's build a faster web.

e-mail

How do I add pointers and math in C++.

First i am creating an array to hold 5 integers, we know that each integer is stored 4 bytes in the computer is memory, some of the new machines might store 8 or more, but in C++ the default of an integer is 4 bytes.

int laurand[5];

I will create three pointers which will be added to the address of &laurand, the first element is [0].

int *landi0 = &laurand[0];
int *landi1 = &laurand[1];
int *landi2 = &laurand[2];

When adding more integers will also add more 4 bytes memory to each address.

cout << "Landi1 is at the address " << landi0 << endl;
cout << "Landi2 is at the address " << landi1 << endl;
cout << "Landi3 is at the address " << landi2 << endl;

I'm adding more two at the address landi0.

landi0 += 2;

Print out on the screen the address of Landi0.

cout << "Landi1 now is at the address " << landi0 << endl;

Watch live in action creating the program, or scroll below to copy the entire code.

Pointers and Math In C++
How do I use pointers and math in C++.
#include <iostream> using namespace std; int main() { int laurand[5]; int *landi0 = &laurand[0]; int *landi1 = &laurand[1]; int *landi2 = &laurand[2]; cout << "Landi0 is at the address " << landi0 << endl; cout << "Landi1 is at the address " << landi1 << endl; cout << "Landi2 is at the address " << landi2 << endl; landi0 += 2; cout << " Landi0 now is at the address " << landi0 << endl; return 0; }

Feel free to use the code.

Last update .

Navigate between pages.
Top