I have an array with 10 int
elements, and I want to point a pointer to this array, not with full size but half. By this, I can reach the first 5 elements by using ptr
and the second 5 elements by increasing pointer one ptr++
.
I just want to know how can I CAST, I don't need to know workarounds or union or struct or anything else... I just wonder the syntax of such a thing.
Here is the example that I struggle with ;
// Pointer to an integer
int *p;
// Pointer to an array of 5 integers
int (*ptr)[5]; // this could be void*
int arr[10];
// Points to 0th element of the arr.
p = arr;
// Points to the whole array arr.
ptr = (int[5]*)&arr; // when arr sized as 5 , ptr = &arr; gives result a pointer "ptr" with size of 5
printf("p = %p, ptr = %p\n", p, ptr);
p++;
ptr++;
printf("p = %p, ptr = %p\n", p, ptr);
return 0;
Note: The answer is: ptr = (int(*)[5])&arr; (compiler's warning message helped me out to find this answer, it was not able to convert one to another type ... )
But I really don't know what is the () and why it is not the same thing as int*[5]. I really don't understand the purpose of parenthesis there.