For starters according to the C Standard the function main without parameters shall be declared like
int main( void )
In this function call
arr = swap_elements(arr, i);
the array designator arr
is implicitly converted to pointer to its first element that has the type char ( * )[MAX_LEN]
.
Also arrays do not have the assignment operator. So this statement
arr = swap_elements(arr, i);
is incorrect.
You need to declare the function like
void swap_elements( char ( *arr )[MAX_LEN], int i);
Within the function you are trying to assign a pointer to the object of the type char
char temp[5];
*temp = *arr;
that does not make a sense.
Instead you need to copy strings stored in character arrays.
The function can be defined the following way
#include <string.h>
void swap_elements( char ( *arr )[MAX_LEN], int i)
{
char temp[MAX_LEN];
strcpy( temp, *arr );
strcpy( *arr, *( arr + i );
strcpy( *( array + i ), temp );
}
Another approach is to declare an array of pointers instead of the multi-dimensional array.
For example
char * arr[] = {"user1", "user2", "user3", "user4"};
In this case the function will look like
void swap_elements( char **arr, int i)
{
char *temp;
temp = *arr;
*arr = *( arr + i );
*( array + i ) = temp;
}
Pay attention to that you need to check within the function that the passed index is not greater than the array size. Otherwise the function is unsafe. So you need to add one more parameter to the function that will specify the number of elements in the passed array.