For starters this function declaration
char[] Reverse(char ch[]);
is incorrect. Functions may not have an array return type.
You could declare the function like
char * Reverse(char ch[]);
but as your function does not deal with strings then in this case it is more logical consistent to declare your function like
void Reverse( char ch[], size_t n );
As the function parameter of the array type is adjusted by the compiler to pointer to the element type of the array like
void Reverse( char *ch, size_t n );
then within the function the parameter ch
is a pointer. As a result this expression sizeof( ch )
used within the function yields the size of pointer that depending on the used system is equal to either 4 or 8. so it is why I specified the second parameter of the function because you need explicitly to pass the array size if the array is not passed to the function by reference.
Within the function you are trying to return pointer to the first element of a local array
char arr[sizeof(ch)];
//...
return arr;
that makes the returned pointer invalid because the array will not be alive after exiting the function.
As the first parameter of the function is declared without the qualifier const then it means that the function needs to reverse the original array instead of making a reversed copy of the original array.
Pay attention to that this statement in main
cout << ch2[0] << endl;
does not make great sense. It outputs only the first element of the array.
Taking all this into account the function can be defined the following way
#include <iostream>
#include <utility>
void Reverse( char *s, size_t n )
{
for ( size_t i = 0; i < n / 2; i++ )
{
std::swap( s[i], s[n-i-1] );
}
}
int main()
{
char s[] = { 'a', 'b', 'c' };
std::cout.write( s, sizeof( s ) ) << '\n';
Reverse( s, sizeof( s ) );
std::cout.write( s, sizeof( s ) ) << '\n';
return 0;
}
The program output is
abc
cba
An alternative approach is to write a template function. In this case the second parameter is not required.
Here you are.
#include <iostream>
#include <utility>
template <size_t N>
void Reverse( char ( &s )[N] )
{
for ( size_t i = 0; i < N / 2; i++ )
{
std::swap( s[i], s[N-i-1] );
}
}
int main()
{
char s[] = { 'a', 'b', 'c' };
std::cout.write(s, sizeof( s ) ) << '\n';
Reverse( s );
std::cout.write(s, sizeof( s ) ) << '\n';
return 0;
}
If you want to write a function that reverses a string stored in a character array then the function declaration can look like
char * Reverse( char s[] );
Here is one more demonstrative program.
#include <iostream>
#include <utility>
#include <cstring>
char * Reverse( char *s )
{
for ( size_t i = 0, n = std::strlen( s ); i < n / 2; i++ )
{
std::swap( s[i], s[n-i-1] );
}
return s;
}
int main()
{
char s[] = { 'a', 'b', 'c', '\0' };
std::cout << s << '\n';
std::cout << Reverse( s ) << '\n';
return 0;
}
The program output will be the same as it is shown above that is
abc
cba
Pay attention to that there is standard algorithm std::revrese
that you could use for your original array
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
char s[] = { 'a', 'b', 'c' };
std::cout.write( s, sizeof( s ) ) << '\n';
std::reverse( std::begin( s ), std::end( s ) );
std::cout.write( s, sizeof( s ) ) << '\n';
return 0;
}
If the array contains a string then in general case a call of the algorithm will look the following way
#include <iostream>
#include <algorithm>
#include <cstring>
int main()
{
char s[] = { 'a', 'b', 'c', '\0' };
std::cout << s << '\n';
std::reverse( s, s + std::strlen( s ) );
std::cout << s << '\n';
return 0;
}