sizeof array clarification

后端 未结 6 2233
不知归路
不知归路 2020-12-06 18:34

I am studying for a final tomorrow in C, and have a question regarding the sizeof operator.

Let\'s say the size of an int is 32 bits and a

相关标签:
6条回答
  • 2020-12-06 18:55

    This is sort of an asymmetry in the C syntax. In C it's not possible to pass an array to a function, so when you use the array syntax in a function declaration for one of the parameters the compiler instead reads it as a pointer.

    In C in most cases when you use an array in an expression the array is implicitly converted to a pointer to its first element and that is exactly what happens for example when you call a function. In the following code:

    int bar[] = {1,2,3,4};
    foo(bar);
    

    the array is converted to a pointer to the first element and that is what the function receives.

    This rule of implict conversion is not however always applied. As you discovered for example the sizeof operator works on the array, and even & (address-of) operator works on the original array (i.e. sizeof(*&bar) == 4*sizeof(int)).

    A function in C cannot recevive an array as parameter, it can only receive a pointer to the first element, or a pointer to an array... or you must wrap the array in a structure.

    Even if you put a number between the brackets in the function declaration...

    void foo(int x[4])
    {
        ...
    }
    

    that number is completely ignored by the compiler... that declaration for the compiler is totally equivalent to

    void foo(int *x)
    {
        ...
    }
    

    and for example even calling it passing an array with a different size will not trigger any error...

    int tooshort[] = {1,2,3};
    foo(tooshort);  /* Legal, even if probably wrong */
    

    (actually a compiler MAY give a warning, but the code is perfectly legal C and must be accepted if the compiler follows the standard)

    If you think that this rule about arrays when in function arguments is strange then I agree, but this is how the C language is defined.

    0 讨论(0)
  • 2020-12-06 18:58

    Because zip is an array and the compiler knows its size at compile-time. It just a case of using the same notation for two different things, something quite usual in C.

    int
    foo (int zap[])
    

    is completely equivalent to

    int
    foo (int *zap)
    

    The compiler doesn't have any idea how big zap could be (so it leaves the task of finding out to the programmer).

    0 讨论(0)
  • 2020-12-06 19:01

    The size of zip is known at compile time and the size of zap is not. That is why you are getting the size of a pointer on sizeof(zap) and the size of the array on sizeof(zip).

    0 讨论(0)
  • 2020-12-06 19:02

    There are some situations wherearrays decay to pointers. Function calls is one of those.

    0 讨论(0)
  • 2020-12-06 19:16

    because it has been statically initialized with 6 elemens.

    0 讨论(0)
  • 2020-12-06 19:18

    zip is a memory block of 6 * sizeof(int) so it has a size of 24 (on your architecture). zap (it could be also written as int *zap in your function declaration) however can point to any memory address and the compiler has no way of knowing how much space starting at this (or even containing this) address has been allocated.

    0 讨论(0)
提交回复
热议问题