hello all,
I am reading a book on c++. It talks about pointers and how to use them. I dont understand what the point of pointers is. Mainly, why would someone need them and what can a programmer do with them. Please be as detailed as possible as i am just a beginner. Please explain the & and * operators.
this is one of the examples given in my book:
// pointer.cpp -- our first pointer variable
#include <iostream>
int main()
{
using namespace std;
int updates = 6; // declare a variable
int * p_updates; // declare pointer to an int
p_updates = &updates; // assign address of int to pointer
// express values two ways
cout << “Values: updates = “ << updates;
cout << “, *p_updates = “ << *p_updates << endl;
// express address two ways
cout << “Addresses: &updates = “ << &updates;
cout << “, p_updates = “ << p_updates << endl;
// use pointer to change value
*p_updates = *p_updates + 1;
cout << “Now updates = “ << updates << endl;
return 0;
the out put of the code is:
Values: updates = 6, *p_updates = 6
Addresses: &updates = 0x0065fd48, p_updates = 0x0065fd48
Now updates = 7
also, i dont understand why "*p_updates" is needed when you already have "updates' since aren't they the same thing (same value).
i am having a hard time understanding pointers, please kindly explain.
i am having a