| | |
C++ pointers problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2005
Posts: 25
Reputation:
Solved Threads: 0
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;
}
#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;
}
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]
[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!
•
•
Join Date: Jan 2005
Posts: 25
Reputation:
Solved Threads: 0
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;
}
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;
}
•
•
Join Date: Jan 2005
Posts: 25
Reputation:
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 <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;
}
#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;
}
•
•
•
•
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 );![]() |
Similar Threads
- Purpose of Pointers? (C++)
- C++ Object Pointers Problem (C++)
- i really hate pointers (C++)
Other Threads in the C++ Forum
- Previous Thread: .h/.C ? Test pgm ... what goes where?
- Next Thread: Linux compilers
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






