So I'm trying to learn C right now, and I have two functions: one that shifts elements in an array:
void shift(int a[], int n) {
int i;
for(i = 0; i != n-1; i++){
a[i] = a[i+1];
}
}
and a version of the same function, except pointer-based:
void pointer_shift(int *a[], int n) {
int i;
for (i = 0; i != n - 1; i++) {
*a[i] = *a[i + 1];
}
}
I don't know whether the pointer-based version is correct or not, but I guess my most important question is how I'm supposed to actually test that both/either work. Besides the definition of these two functions, I have:
#include <stdio.h>
void shift(int a[], int n);
void pointer_shift(int *a[], int n);
int main(void) {
printf("Begin execution of testing Problem 1\n");
int a1[] = {100, 101, 102};
int i;
for(i = 0; i<3;i++)
printf("Before Shift: " "%d\n", a1[i]);
//shift(a1, 3);
pointer_shift(&a1, 3);
for(i = 0; i<3;i++)
printf("After Shift In Main: " "%d\n", a1[i]);
return 0;
}
shift(a1, 3)
works fine, but I, for the life of me, can't figure out how to correctly test pointer_shift.
I get two errors; one is that in the line
pointer_shift(&a1, 3)
I am passing argument 1 from an incompatible pointer type. The other error is indecipherable, but I was hoping the problem would be obvious enough that someone would be able to help me. So... how to test my two functions in my main?
Thanks!