Yes, essentially, the indexing notation is a shorthand for pointer arithmetic. By default, the variable int *a points to the first index in that array. Technically, int *a is just a pointer to an integer, and you just 'happen to know' that other integers in memory follow it. And thus they have given us a convenient notation
a[i] // where i is the number of spaces passed point *a we want to look.
I am not sure what you are trying to do within the loop, but to access the ith element, you would do the following. My function just takes the compliment of the array a. nothing is done to c.
#include <stdio.h>
void set_compliment(int *a, int *compliment, int n) {
int i;
for (i = 0; i < n; i++) {
// if (a[i] == 0)
if ( *(a + i) == 0) {
// a[i] = 1;
*(a + i) = 1;
// else if (a[i] == 1)
} else if ( *(a+i) == 1) {
// a[i] = 0;
*(a + i) = 0;
}
}
}
//------------------------------------------------------------------------------
void display(int *a, int n) {
int i;
for (i = 0; i < n; i++) {
printf("%i ", a[i]);
}
printf("\n");
}
//------------------------------------------------------------------------------
int main() {
int a[] = {1, 1, 1, 1, 0};
int c[] = {0, 0, 0, 0, 1};
// Get length of array
int n = sizeof(a) / sizeof(a[0]);
set_compliment(a, c, n);
display(a, n);
display(c, n);
return 0;
}
pand indexi, the expression*(p + i)is exactly equal top[i].