pointers - Error at "cout << " in C++ -
i trying reverse char array in c++. here code :
void reverse(char s[]); int main() { char s [] = "harry"; cout << reverse(s) << endl; system("pause"); return 0; } void reverse(char s[]) { if( strlen( s ) > 0 ) { char* first = &s[ 0 ]; char* last = &s[ strlen( s ) - 1 ]; while( first < last ) { char tmp = *first; *first = *last; *last = tmp; ++first; --last; } return; } however, got error @ cout << reverse(s) << endl; line located in main method , have no idea why.the error message no operator match these operands. can me fix this?
thanks in advance.
your reverse function has return type of void. means doesn't return cout << reverse() has nothing output.
seems instead meant do:
char s [] = "harry"; reverse(s); cout << s << endl; alternatively, can make reverse return char* , put return s; @ end of body. however, little strange, since you're using both argument , return value same function output. use above.
of course, more if make use of standard library; use std::string , std::reverse:
std::string s = "test"; std::reverse(s.begin(), s.end()); std::cout << s << std::endl;
Comments
Post a Comment