The first line is clear - the declaration and initialization of the array. But what does the second line do?

short a[] = {1, 4, 8, 5}; (char *) (a + 3) - (char*) a; 

Closed due to the fact that off-topic participants Yuri , Regent , Kromster , αλεχολυτ , Pavel Mayorov Jan 16 '17 at 12:47 .

  • Most likely, this question does not correspond to the subject of Stack Overflow in Russian, according to the rules described in the certificate .
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 15
    Do you sit on the exam? - αλεχολυτ
  • Check with the compiler. What is the problem? - αλεχολυτ
  • five
    This question should be closed, because it looks like: "What will happen as a result?" - Yuri
  • one
    @Majestio, reformulated the question, making it more useful to others. - ߊߚߤߘ
  • one
    @Arhad so it’s necessary to write it down somewhere or output as is. - αλεχολυτ

1 answer 1

The difference between two pointers pointing to the elements of a single array gives the number of array elements located between two pointers.

Or following standard C (6.5.6 Additive operators)

If you are a subtracted object, there are two pointers; the subscripts of the two array elements

This is the so-called pointer arithmetic.

The expression (a + 3) has the pointer type short * and has the value of the address of the fourth element of array a (element a[3] ), that is, it is a pointer to the last element of the array with value 5. Expression a implicitly converted to a pointer to the first element of the array ( the element a[0] ). Both pointers are cast to char * . That is, the original array is interpreted as an array of elements of type char . Therefore, the value of the expression

 (char *) (a + 3) - (char*) a 

there will be a number of bytes (or char elements) between the first element of the source array and the fourth element of the source array, which is calculated using the formula 3 * sizeof( short ) If sizeof( short ) is 2 (the specific value depends on the platform used), then the value of the original expression there will be number 6. That is, before the last element of the array there are 3 elements, each of which occupies 2 bytes, or if the array is interpreted as an array of characters, as is the case in the above expression, this difference gives 6 elements of the array with ments such as char , each of which is 1 byte.

Below is a demo program that shows the difference when short * pointers are used, that is, pointers to the original elements of the array, and when the array elements and, accordingly, pointers to them, are interpreted as having the type char and, accordingly, char * .

 #include <stdio.h> int main( void ) { short a[] = { 1, 4, 8, 5 }; size_t n = 0; for (short *p = a; p + n != a + 3; ) n++; printf( "n = %zu\n", n ); n = 0; for (char *p = (char *)a; p + n != (char *)(a + 3); ) n++; printf("n = %zu\n", n); } 

The value of n after the operation of the first cycle will be equal to 3, since ( a + 3 ) - a is equal to 3, while the value of n after the operation of the second cycle will be equal to 6 , since ( char * )( a + 3 ) - ( char * )a equals 3 * sizeof( short ) .

  • It would be great if you brought the test code to the output of bit values ​​during any "interpretation". IMHO. Well, so, while - plus. - Majestio