#include<stdio.h>
#define MAX_LEN 20
char *swap_elements(char **arr, int i)
{
char temp[5];
*temp = *arr;
*arr = *(arr i);
*(arr i) = *temp;
return arr;
}
void main()
{
char arr[][MAX_LEN]={"user1", "user2", "user3", "user4"};
int i, j;
printf("Enter the index with which you want to swap: ");
scanf("%d", &i);
arr = swap_elements(arr, i);
for(j=0;j<4;j )
printf("%s", *(arr i));
}
This is the code that I have written for swapping the first element with the user-entered index element. But it's showing an error as below
CodePudding user response:
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.
CodePudding user response:
Here is how to actually achieve what you are trying to do.
#include <stdio.h>
#include <string.h>
#define MAX_LEN 20
void swap_elements(char* arr, int i)
{
char temp[MAX_LEN];
strncpy(temp, arr, MAX_LEN);
strncpy(arr, arr i*MAX_LEN, MAX_LEN);
strncpy(arr i*MAX_LEN, temp, MAX_LEN);
}
void main()
{
char arr[][MAX_LEN]={"user1", "user2", "user3", "user4"};
int swapTarget;
printf("Enter the index with which you want to swap: ");
scanf("%d", &swapTarget);
swap_elements(arr, swapTarget);
for(int i=0;i<4;i )
printf("%s", arr[i]);
}
To understand why I did it like this first you need to understand how multidimensional arrays are stored in C.
When you for example have this array:
int arr2d[5][2] = {{1,2},{3,4},{5,6},{7,8},{9,10}}
It will be stored like this:
| 1 | 2 | 4 | 5 | 7 | 8 | 9 | 10 |
And when you try to access the 2nd array 2nd element element with arr2d[1][1]
C basically converts it to: *(arr2d 1*2 1)
. 2 here representing the size of one of the subarrays(in your case MAX_LEN
).
So when I write arr i*MAX_LEN
it is the same as writing arr[i]
but you have to do it manually.
Also you can't move strings around like this *arr = *(arr i)
. This would only move the i-th character of arr into the place of the first character of arr.
Which is why you have to use either strcpy
or strncpy
. The difference being that with strncpy
you define the length of the string to be moved while with strcpy
it copies until it gets to a null-byte.