I had to swap array elements with call by reference to reduce the lines of code in my bigger project, and I am successful in swapping them but this code gives a bunch of warnings, how do I do this efficiently?
#include <stdio.h>
void swap(int *arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
int main(void)
{
int a[5] = {2, 4, 6, 2, 3};
swap(&a, 1, 2);
for (int i = 0; i < 5; i++)
{
printf("%d", a[i]);
}
return 0;
}
OUTPUT =>
swap_array.c: In function 'main':
swap_array.c:11:10: warning: passing argument 1 of 'swap' from incompatible pointer type [-Wincompatible-pointer-types]
11 | swap(&a, 1, 2);
| ^~
| |
| int (*)[5]
swap_array.c:2:16: note: expected 'int *' but argument is of type 'int (*)[5]'
2 | void swap(int *arr, int i, int j)
| ~~~~~^~~
26423