Member Avatar for kohkohkoh
#include <iostream>
using namespace std;

 int main()
 {
int age;

for (int i=0;i<5;i++)
{
cout << "Enter :";
cin >> age;
}

int *sum=&age;


for ( i=0;i<5;i++)
{
 cout <<*(sum+i)  << endl;
}
   return 0;
 }

here is one of my program that i wrote.
i do not know whats the problem with it.
it seems can't display back the values that i've entered in.
can sir/miss help me to correct my mistake(s).
Thank you!

Try:

cout <<(*sum+i) << endl;

You want the asterisk in front of the pointer you want to dereference, not in front of the expression. What you were saying was:

"print out the contents of the int pointer at location (sum + i)" which is the same as saying

cout << sum << endl;

int age;

You have age declared as a single int, but later try to operate on it as if it were an array of ints, which it is not.

An array is declared:
datatype array_name[int_size];

Eg. int vals[10];

int *p = vals;

Then p[0] = vals[0] and p[1] = vals[1] and so on.
Also you can access the vals by *(p+i) instead of the [], and the expression is read as the value at the address of p+i.

An array var is also a pointer so you dont need a & in front of it when you assign it to another pointer.But if you want to just point to a single int for example:

int *p = &val; //means p = address of val

and you can access the value a the address pointed to by p usinf another_int = *p ;

---Helps???

Member Avatar for kohkohkoh

thank you everyone ;)

#include <iostream>
using namespace std;

int main()
{
int age;

for (int i=0;i<5;i++)
{
cout << "Enter :";
cin >> age;
}

int sum=age;

for (int  i=0;i<5;i++)
{
cout <<sum+i<< endl;
}

int name;
cin>>name;
}

Salam friends.this is me faizan ahmed, this program just prints the values which u entered before..as one of my friend was asking to print those entered values so i found this way ;)

commented: Almost 6 years too late! Don't bump old threads. -1

6years too late and ignoring rules.

Thread closed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.