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
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
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
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?
It means your prototype and function would take references instead of pointers.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
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
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
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
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
[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
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
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
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314