2

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!

1
  • This is off topic - but when you use C++11, it is better to use std::array over pointers to array. Commented Sep 9, 2012 at 9:19

4 Answers 4

3

Change your pointer based function like this:

void pointer_shift(int *a, int n) {
   int i;
   for (i = 0; i != n - 1; i++) {
      *(a+i) = *(a+i+1);
   }
}

What you are receiving is an array of pointers whereas you pass an array from main. Since an array decays into pointer, calling shift(a1, 3) is sufficient here.

Sign up to request clarification or add additional context in comments.

Comments

3

Change the function prototype to accept an int*

void pointer_shift(int *a, int n) 

and then access array elements like this

*(a+1) = 4;

which is the same as

a[1] = 4;

you can still call it the same way using the array variable since arrays decay to a pointer to their first element when passed as a parameter to a function

Comments

0
int main(void) {
    printf("Begin execution of testing Problem 1\n");
    int a1[] = {100, 101, 102};
    int *array_of_pointers[3];
    int i;

   array_of_pointers[0]= &a1[0];
   array_of_pointers[1]= &a1[1];
   array_of_pointers[2]= &a1[2];

   for(i = 0; i<3;i++)
       printf("Before Shift: " "%d %p\n ", a1[i], array_of_pointers[i]);

/*
  same as passing a1, the address of the array is
  the same value as the value of the array, just a different type
*/
   pointer_shift(array_of_pointers, 3);
   printf("After Shift, note that array_of_pointers has not shifted:\n");
   for(i = 0; i<3;i++)
       printf("After Shift In Main: " "%d %p\n", a1[i], array_of_pointers[i]);
   return 0;
}

pointer_shift expects an array of pointers, not an array of int. the "shift" here is not in the array, but rather in the values of what the elements in the array are pointing to, the values of the elements of the array remain the same!

Comments

-1

Your first function is already pointer-based. When you define a function parameter of type int[], the compiler treats it the same as if you defined a pointer type int*. So the function can be passed either a pointer or an array (which will be automatically cast to a pointer).

If you change the function prototype to accept int *a instead of int a[], the function will work exactly the same.

Your second function doesn't work because it expects an array of pointers to int, which isn't what you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.