Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

12
  • Have been looking for quite a long time for this answer. Thank you! And if you know, could you tell a little further what an array expression is. If I use sizeof, do i count the size of only the elements of the array? Then the array “head” also takes up space with the information about length and a pointer (and this means that it takes more space, than a normal pointer would)? Commented Dec 7, 2017 at 21:51
  • And one more thing. An array of length 5 is of type int[5]. So that is from where we know the length when we call sizeof(array) - from its type? And this means that arrays of different length are like different types of constants? Commented Dec 9, 2017 at 13:09
  • @AndriyDmytruk: sizeof is an operator, and it evaluates to the number bytes in the operand (either an expression denoting an object, or a type name in parentheses). So, for an array, sizeof evaluates to the number of elements multiplied by the number of bytes in a single element. If an int is 4 bytes wide, then a 5-element array of int takes up 20 bytes. Commented Dec 9, 2017 at 23:40
  • 1
    @Stan: The subscript operation a[i] is defined as *(a + i) - given a starting address a, find the address of the i'th object in the array, and dereference the result. If a is an expression of array type, it will be converted to a pointer before the addition is performed. Remember that in pointer arithmetic, a + 1 yields the address of the next object of the pointed to type, not the next byte. If a points to a 4-byte int, then a + 1 points to the next 4-byte int. If a points to a 128-byte struct, then a + 1 points to the next 128-byte struct. Commented Jun 21, 2018 at 17:34
  • 2
    @Stan: The expression a has type int [2][3], which "decays" to type int (*)[3]. The expression *(a + 1) has type int [3], which "decays" to int *. Thus, *(*(a + 1) + 2) will have type int. a points to the first 3-element array of int, a + 1 points to the second 3-element array of int, *(a + 1) is the second 3-element array of int, *(a + 1) + 2 points to the third element of the second array of int, so *(*(a + 1) + 2) is the third element of the second array of int. How that gets mapped to machine code is entirely up to the compiler. Commented Jun 21, 2018 at 21:03