For starters this function declaration
void reverse(char *rTarget[]);
is equivalent to
void reverse(char **rTarget);
There is no great sense to declare it such a way.
You should declare the function like
void reverse( char rTarget[] );
and call it like
reverse( memes );
The function definition is also wrong. At least you have to use the loop like
for (int i = 0; i < length / 2; i++)
and these expressions
swap = *rTarget[i];
*rTarget[i] = *rTarget[length - i];
*rTarget[length - i] = swap;
are invalid. They shall be rewritten like
swap = ( *rTarget )[i];
( *rTarget )[i] = ( *rTarget )[length - i -1];
( *rTarget )[length - i - 1] = swap;
Also the variable length
shall have the type size_t
because in general an object of the type int can not accommodate an object of the type size_t and the function strlen
have the return type size_t
. So you have to substitute this declaration
int length = (int) strlen(*rTarget);
For this declaratiuon
size_t length = strlen(*rTarget);
Pay attention to that according to the conventions for standard C string functions the function should return pointer to the reversed string.
And according to the C Standard the function main without parameters shall be declared like
int main( void )
Here is a demonstration program that shows how the function can be declared and defined
#include <stdio.h>
#include <string.h>
char * reverse( char *s )
{
for ( size_t i = 0, n = strlen( s ); i < n / 2; i++ )
{
char c = s[i];
s[i] = s[n-i-1];
s[n-i-1] = c;
}
return s;
}
int main(void)
{
char memes[] = "memes";
puts( reverse( memes ) );
return 0;
}
The program output is
semem
char* rTarget[]
expression means "an array ofchar*
". You must pass the char array (it's basically also a pointer) then modify it. It will not be cloned. – CoderCharmander Oct 29 at 13:33