r123454321
r123454321

Reputation: 3403

Arrays & pointers in C

So I have the following code snippet:

#include <stdio.h>

void pointer_shift(int *a, int n);

int main(void) {
    int a[] = {100, 101, 102};
    pointer_shift(a1, 3);
}

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

I just want to clarify how the pointers work in this snippet. So pointer_shift takes in 'a', a pointer to an int, correct? a1 is passed in to this parameter, and since arrays decay to a pointer to their first element, it works.

First of all, hopefully what I said in the above paragraph is correct. Secondly, what does *(a + i) = *(a + i + 1); actually do? Say we're on the first iteration of the for loop, and i = 0. Then the left side, *a, accesses what, exactly? Does it represent a pointer? I thought * was the dereferencing operator, and accesses the object that a pointer points to... And so then it sets *a = *(a + 1). (a + 1) is the next element in the array, but what exactly does this assignment do, and why?

Thanks!

Upvotes: 3

Views: 232

Answers (4)

Alex Reynolds
Alex Reynolds

Reputation: 96974

The expression a + i is pointer arithmetic, incrementing the memory address stored in a by i units of the pointer size of a. So if a pointer to an int takes four bytes on your system, and if the current memory address is, say, 0x1234 the value of a + 1 would be 0x1238.

What the asterisk does is dereference that address and access the actual value at that address. If you have 100 stored at a or a[0], and 101 stored at a + 1 or a[1], then *(a + i) = *(a + i + 1) replaces 100 with 101 at a[0], for i = 0.

Basically, you want to read this C tutorial on pointers and arrays.

Upvotes: 0

Ed Swangren
Ed Swangren

Reputation: 124770

I just want to clarify how the pointers work in this snippet. So pointer_shift takes in 'a', a pointer to an int, correct? a1 is passed in to this parameter, and since arrays decay to a pointer to their first element, it works.

Yes, when you pass an array to a function it degrades to a pointer. An array is not a pointer in an object sense, but it is a pointer in a value sense. When you pass it to a function its value is passed, i.e., a pointer to the first element.

array indexing is the same as pointer arithmetic, so the last two lines in this snippet are equivalent:

int arr[] = {1, 2, 3};

arr[0] = 10;
*arr = 10;

as are these:

arr[1] = 20;
*(arr + 1) = 20;

Upvotes: 0

pb2q
pb2q

Reputation: 59637

*(a + i) = *(a + i + 1); is copying array elements within the array using a bit of pointer arithmetic.

*(a + i) is equivalent to a[i], so the statement is equivalent to a[i] = a[i + 1];. The loop is moving the array values "to the left" in the array: a[0] = a[1]; a[1] = a[2]; and so on.

Your understanding of the function call is correct.

Upvotes: 0

Baiyan Huang
Baiyan Huang

Reputation: 6781

It is actually not pointer shift, but value shift, *(a+i) is of same effect as a[i], so what it does is a[i] = a[i+1]

Upvotes: 2

Related Questions