C++ pointers problem

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

Join Date: Jan 2005
Posts: 25
Reputation: seeplusplus is an unknown quantity at this point 
Solved Threads: 0
seeplusplus seeplusplus is offline Offline
Light Poster

C++ pointers problem

 
0
  #1
Jan 16th, 2005
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;
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 25
Reputation: seeplusplus is an unknown quantity at this point 
Solved Threads: 0
seeplusplus seeplusplus is offline Offline
Light Poster

Re: C++ pointers problem

 
0
  #2
Jan 16th, 2005
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
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: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: C++ pointers problem

 
0
  #3
Jan 16th, 2005
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".
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,062
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 936
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: C++ pointers problem

 
0
  #4
Jan 16th, 2005
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]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 25
Reputation: seeplusplus is an unknown quantity at this point 
Solved Threads: 0
seeplusplus seeplusplus is offline Offline
Light Poster

Re: C++ pointers problem

 
0
  #5
Jan 16th, 2005
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;
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 25
Reputation: seeplusplus is an unknown quantity at this point 
Solved Threads: 0
seeplusplus seeplusplus is offline Offline
Light Poster

Re: C++ pointers problem

 
0
  #6
Jan 16th, 2005
yea, the syntax error is in function main, before return.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 10
Reputation: Emmitt310 is an unknown quantity at this point 
Solved Threads: 0
Emmitt310 Emmitt310 is offline Offline
Newbie Poster

Re: C++ pointers problem

 
0
  #7
Jan 16th, 2005
All i see in your code that could give a syntax error is the lack of a semicolon after the function call order_chars.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 25
Reputation: seeplusplus is an unknown quantity at this point 
Solved Threads: 0
seeplusplus seeplusplus is offline Offline
Light Poster

another problem

 
0
  #8
Jan 18th, 2005
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;
}
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,388
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: 244
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: another problem

 
0
  #9
Jan 18th, 2005
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 );
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 25
Reputation: seeplusplus is an unknown quantity at this point 
Solved Threads: 0
seeplusplus seeplusplus is offline Offline
Light Poster

Re: C++ pointers problem

 
0
  #10
Jan 18th, 2005
i have to change my code above to use reference variables instead, what does this mean?
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC