3

I have a question about using pointers. Can you please tell me the difference between these two pointers?

int array[10] = {1, };
int (*pointer1)[10] = &array;
int *pointer2 = array;

I'm confused because these two pointers can do the exactly same thing... like accessing to the element like pointer1[0] or pointer2[0], I understand the pointer2 is an element pointer and pointer1 is an pointer that is pointing to the whole array. But I don't know why programers are distinguish these two.

Gazelgoes
  • 43
  • 4

2 Answers2

4

In most contexts, array names decay to pointers. In simple words, array names are converted to pointers. That's the reason why you can use pointers to access elements of arrays. However, you should remember that pointers and arrays are not the same.

since arrays in C can decays to pointer with the same data type, (and the name of the array can decays to the address of the array or the address of the first element the same)

we can write:

int* ptr = array;

ptr is new data type which poits to the array start.

now what is common: pointer1 and pointer 2 hold the same address points in memory to same address.

but there is some difference between them:

pointer1 is pointer to an array of 10 integer data types where pointer2 is pointer to one integer data type.

when derefrence the pointers: pointer2 deref to one int data type where pointer1 is deref to an array of 10 ints. so we can do with pointer1:

(*pointer1)[5]=1000;

but this will be in valid in pointer2.

the equavivalnt in pointer2 will be:

*(pointer2+5)=1000;

pointer arithmetics are valid on both, but the results of increment are different:

int* ptr_new = pointer1 + 1; 

increments pointer1 by sizeof the whole array (10 ints) and now ptr_new points to the last element.

int* ptr_new = pointer2 + 1;

increments pointer2 by sizeof(int) and now ptr_new points to the second element in the array.

Adam
  • 2,820
  • 1
  • 13
  • 33
2

arrays decay to pointers to the reference to its first element.

int array[10];

array decays to the pointer to int &array[0] decays to the pointer to int &array[ decays to the pointer to the 10 elements int array

what is the difference between the pointer to int and pointer to the int array? they reference different type objects and their arithmetic is different as per example:

int array[10] = {1, };
int (*pointer1)[10] = &array;
int *pointer2 = array;

int main(void)
{
    printf("sizeof(int) is: %zu, sizeof(*pointer2) is: %zu, sizeof(*pointer1) is: %zu\n", sizeof(int), sizeof(*pointer2), sizeof(*pointer1));

    printf("pointer1 = %p,  (pointer1 + 1) = %p, (pointer1 + 1) - pointer1 = %zu\n", (void *)pointer1, (void *)(pointer1 + 1), (uint8_t *)(pointer1 + 1) - (uint8_t *)(pointer1));
    printf("pointer2 = %p,  (pointer2 + 1) = %p, (pointer2 + 1) - pointer2 = %zu\n", (void *)pointer2, (void *)(pointer2 + 1), (uint8_t *)(pointer2 + 1) - (uint8_t *)(pointer2));
}

and the result:

sizeof(int) is: 4, sizeof(*pointer2) is: 4, sizeof(*pointer1) is: 40
pointer1 = 0x404040,  (pointer1 + 1) = 0x404068, (pointer1 + 1) - pointer1 = 40
pointer2 = 0x404040,  (pointer2 + 1) = 0x404044, (pointer2 + 1) - pointer2 = 4

https://godbolt.org/z/BwbfQy

0___________
  • 60,014
  • 4
  • 34
  • 74