943,516 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7419
  • C++ RSS
Aug 26th, 2004
0

*Pointer program problem.

Expand Post »
#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!
Similar Threads
koh
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
koh is offline Offline
73 posts
since Jul 2004
Aug 26th, 2004
0

Re: *Pointer program problem.

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;
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Aug 26th, 2004
0

Re: *Pointer program problem.

C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 26th, 2004
0

Re: *Pointer program problem.

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???
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Aug 27th, 2004
0

Re: *Pointer program problem.

thank you everyone
koh
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
koh is offline Offline
73 posts
since Jul 2004
Apr 23rd, 2010
-2

ZON

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int age;
  7.  
  8. for (int i=0;i<5;i++)
  9. {
  10. cout << "Enter :";
  11. cin >> age;
  12. }
  13.  
  14. int sum=age;
  15.  
  16. for (int i=0;i<5;i++)
  17. {
  18. cout <<sum+i<< endl;
  19. }
  20.  
  21. int name;
  22. cin>>name;
  23. }



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
Last edited by peter_budo; Apr 24th, 2010 at 6:16 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
ZON
Reputation Points: 9
Solved Threads: 0
Newbie Poster
ZON is offline Offline
2 posts
since Apr 2010
Apr 24th, 2010
2
Re: *Pointer program problem.
6years too late and ignoring rules.

Thread closed
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,653 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: Removing digits, non letters from a string
Next Thread in C++ Forum Timeline: how to develop a c++ program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC