954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ pointers problem

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

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;
}

seeplusplus
Light Poster
25 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

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

seeplusplus
Light Poster
25 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

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".

Chainsaw
Posting Pro in Training
436 posts since Jun 2004
Reputation Points: 36
Solved Threads: 11
 

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

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]

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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

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;
}

seeplusplus
Light Poster
25 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

yea, the syntax error is in function main, before return.

seeplusplus
Light Poster
25 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

All i see in your code that could give a syntax error is the lack of a semicolon after the function call order_chars.

Emmitt310
Newbie Poster
10 posts since Sep 2004
Reputation Points: 10
Solved Threads: 0
 

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

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;
}

seeplusplus
Light Poster
25 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 
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 );
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

i have to change my code above to use reference variables instead, what does this mean?

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

It means your prototype and function would take references instead of pointers.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

alright, i think i changed the above program to call by reference, here it is to just make sure though.


#include

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;

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;
}

seeplusplus
Light Poster
25 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 
int order_chars( char*, char*, char* );


This is not call by reference in the C++ sense. It would be like this.

int order_chars( char&, char&, char& );


And you wouldn't use the& on each variable as it is being passed to the function. And in your function you wouldn't dereference the (non) pointer with * in the cout statement.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

alright i made those changes, but the compiler gives me a error, it says
/tmp/ccpTbg2D.o(.text+0x69): In function `main':
: undefined reference to `order_chars(char&, char&, char&)'
collect2: ld returned 1 exit status

heres the new code:


#include

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;

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;
}

seeplusplus
Light Poster
25 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

The compiler might be kindly telling you that if you change the function declaration, you ought to also change the function definition so that they both indeed refer to they same function.

Otherwise, it might assume you are telling it of the existance of one function that you do not define or provide; while at the same time providing it with a function that the linker does not use.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

so how can i fix this problem?

seeplusplus
Light Poster
25 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

[Added code tags and highlighting to your quoted material.]alright i made those changes, but the compiler gives me a error, it says

/tmp/ccpTbg2D.o(.text+0x69): In function `main':
: undefined reference to `order_chars(char&, char&, char&)'
collect2: ld returned 1 exit status


heres the new code:

#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;

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;
}

See how you only changed the declaration but not the definition.The compiler might be kindly telling you that if you change the function declaration, you ought to also change the function definition so that they both indeed refer to they same function.Make the function definition match your prototype.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

alright, got it...thanks!

seeplusplus
Light Poster
25 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

ok, im reading about how you can change this program using an array as an argument, i think i should know how to do this, just in case we use it in the class im taking. so far i initialized an array, how can you use an array variable as an argument?
heres my code:

#include

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 array[ 3 ] = { c1, c2, c3 };

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

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;
}

seeplusplus
Light Poster
25 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 
ok, im reading about how you can change this program using an array as an argument
#include <iostream>

void order_chars ( const char (&array) [ 3 ] )
{
   for ( int i = sizeof array - 1; i >= 0; --i )
   {
      std::cout << array [ i ];
   }
   std::cout << std::endl;
}

int main()
{
   char c [ 3 ];
   std::cout << "Enter " << sizeof c << " characters: ";
   for ( int i = 0; i < sizeof c; ++i )
   {
      std::cin >> c [ i ];
   }
   order_chars ( c );
   return 0;
}

/* my input/output
Enter 3 characters: abc
cba
*/


???[Jul, please tell me where I have erred. Thanks.]
:)

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You