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

Q > why "*p_updates" is needed when you already have "updates' since aren't they the same thing (same value).

Ans : yes they r same , this program demonstrate u how may ways u can access the value..

now if u have some variable say int a = 5;

what does that mean?

u r storing value 5 at some variable a;
now what is this variable a?
its a name to some memory location say OX257D3

now what does a pointer do?
a pointer can store addresses like mentioned above

now if u do
say int *p = &a;

means u r storing the address mentioned abover in this pointer.
now u can access 'a' using this pointer.

use :
across the functions
read about passing parameters to functions.
read about pass by reference

there are many more benifits of having a pointer that u need to study as one cant explain them here.

This article has been dead for over six months. Start a new discussion instead.