944,067 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6451
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 16th, 2005
0

C++ pointers problem

Expand Post »
Hello, i have to write a program using C++ that will store 3 characters into 3 variables, and using pointers i have to rearrange the order in a function. so if c1, c2, c3 have the value 2,4,6....using pointers i have to switch the values of c1, c2, and c3 to 6,4,2. Here is what i have written so far, but it wont compile correctly, can anyone help?


#include <iostream>

int order_chars( char*, char*, char* );
int main()
{

char c1 = 5;
char c2 = 10;
char c3 = 15;
char* c1ptr;
char* c2ptr;
char* c3ptr;

c1ptr = &c3;
c2ptr = &c2;
c3ptr = &c1;

order_chars ( &c1, &c2, &c3 )


return 0;
}


int order_chars( char c3ptr, char c2ptr, char c1ptr )
{
std::cout << c1ptr << c2ptr << c3ptr << std::endl;

return 0;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
seeplusplus is offline Offline
25 posts
since Jan 2005
Jan 16th, 2005
0

Re: C++ pointers problem

the only problem i have with the program i wrote above is that the compiler says there a problem, can anyone help me find what is wrong with this program? the program matches the directions above, but theres a problem. please help
Reputation Points: 10
Solved Threads: 0
Light Poster
seeplusplus is offline Offline
25 posts
since Jan 2005
Jan 16th, 2005
0

Re: C++ pointers problem

Well, for one thing order_chars is declared twice, once with char* and once with char parameters.

It would be helpful to tell us what the compiler actually SAYS rather than "it says there a problem".
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Jan 16th, 2005
0

Re: C++ pointers problem

Compare this code to your code and it will give you a hint why yours does not work.
[php]#include <iostream>

int order_chars( int*, int*, int* );


int main()
{
int c1 = 5;
int c2 = 10;
int c3 = 15;
int* c1ptr;
int* c2ptr;
int* c3ptr;

c1ptr = &c3;
c2ptr = &c2;
c3ptr = &c1;

order_chars (c3ptr, c2ptr, c1ptr);

std::cin.get(); //wait
return 0;
}


int order_chars( int* c3ptr, int* c2ptr, int* c1ptr )
{
std::cout << *c1ptr << *c2ptr << *c3ptr << std::endl;

return 0;
}
[/php]
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jan 16th, 2005
0

Re: C++ pointers problem

ok heres my new code, but it still says on the compiler that there is a syntax error on line before return i think in the main function.
new code...


#include <iostream>

int order_chars( char*, char*, char* );
int main()
{

char c1 = 5;
char c2 = 10;
char c3 = 15;
char* c1ptr;
char* c2ptr;
char* c3ptr;

c1ptr = &c3;
c2ptr = &c2;
c3ptr = &c1;

order_chars ( c3ptr, c2ptr, c3ptr )


return 0;
}


int order_chars( char* c3ptr, char* c2ptr, char* c1ptr )
{
std::cout << c1ptr << c2ptr << c3ptr << std::endl;

return 0;
}
Reputation Points: 10
Solved Threads: 0
Light Poster
seeplusplus is offline Offline
25 posts
since Jan 2005
Jan 16th, 2005
0

Re: C++ pointers problem

yea, the syntax error is in function main, before return.
Reputation Points: 10
Solved Threads: 0
Light Poster
seeplusplus is offline Offline
25 posts
since Jan 2005
Jan 16th, 2005
0

Re: C++ pointers problem

All i see in your code that could give a syntax error is the lack of a semicolon after the function call order_chars.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Emmitt310 is offline Offline
10 posts
since Sep 2004
Jan 18th, 2005
0

another problem

alright, say i enter a 1,2,3 when my program asks me, when im printing out the contents of the pointers in the function order_chars, it prints 1,2,1....instead it should print 3,2,1....cause the function reorders the characters i entered around. heres my code, can you see the problem?


#include <iostream>

int order_chars( char*, char*, char* );
int main()
{

char c1;
char c2;
char c3;

std::cout << "Enter three characters\n";
std::cin >> c1 >> c2 >> c3;

char* c1ptr;
char* c2ptr;
char* c3ptr;

c1ptr = &c3;
c2ptr = &c2;
c3ptr = &c1;

order_chars ( c3ptr, c2ptr, c3ptr );


return 0;
}


int order_chars( char* c3ptr, char* c2ptr, char* c1ptr )
{
std::cout << * c1ptr << * c2ptr << * c3ptr << std::endl;

return 0;
}
Reputation Points: 10
Solved Threads: 0
Light Poster
seeplusplus is offline Offline
25 posts
since Jan 2005
Jan 18th, 2005
0

Re: another problem

Quote originally posted by seeplusplus ...
alright, say i enter a 1,2,3 when my program asks me, when im printing out the contents of the pointers in the function order_chars, it prints 1,2,1....instead it should print 3,2,1....cause the function reorders the characters i entered around. heres my code, can you see the problem?
order_chars ( c3ptr, c2ptr, c3ptr );
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jan 18th, 2005
0

Re: C++ pointers problem

i have to change my code above to use reference variables instead, what does this mean?
Reputation Points: 10
Solved Threads: 0
Light Poster
seeplusplus is offline Offline
25 posts
since Jan 2005

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.
Message:
Previous Thread in C++ Forum Timeline: .h/.C ? Test pgm ... what goes where?
Next Thread in C++ Forum Timeline: Linux compilers





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


Follow us on Twitter


© 2011 DaniWeb® LLC