*Pointer program problem.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2004
Posts: 65
Reputation: koh is an unknown quantity at this point 
Solved Threads: 0
koh koh is offline Offline
Junior Poster in Training

*Pointer program problem.

 
0
  #1
Aug 26th, 2004
#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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: *Pointer program problem.

 
0
  #2
Aug 26th, 2004
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[i] << endl;
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: *Pointer program problem.

 
0
  #3
Aug 26th, 2004
  1. 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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: *Pointer program problem.

 
0
  #4
Aug 26th, 2004
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???
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 65
Reputation: koh is an unknown quantity at this point 
Solved Threads: 0
koh koh is offline Offline
Junior Poster in Training

Re: *Pointer program problem.

 
0
  #5
Aug 27th, 2004
thank you everyone
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC