In C, are arrays pointers or used as pointers?

前端 未结 6 1058
野趣味
野趣味 2020-11-22 05:23

My understanding was that arrays were simply constant pointers to a sequence of values, and when you declared an array in C, you were declaring a pointer and allocating spac

6条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 05:52

    Arrays are not pointers, though in most expressions an array name evaluates to a pointer to the first element of the array. So it is very, very easy to use an array name as a pointer. You will often see the term 'decay' used to describe this, as in "the array decayed to a pointer".

    One exception is as the operand to the sizeof operator, where the result is the size of the array (in bytes, not elements).

    A couple additional of issues related to this:

    An array parameter to a function is a fiction - the compiler really passes a plain pointer (this doesn't apply to reference-to-array parameters in C++), so you cannot determine the actual size of an array passed to a function - you must pass that information some other way (maybe using an explicit additional parameter, or using a sentinel element - like C strings do)

    Also, a common idiom to get the number of elements in an array is to use a macro like:

    #define ARRAY_SIZE(arr) ((sizeof(arr))/sizeof(arr[0]))
    

    This has the problem of accepting either an array name, where it will work, or a pointer, where it will give a nonsense result without warning from the compiler. There exist safer versions of the macro (particularly for C++) that will generate a warning or error when it's used with a pointer instead of an array. See the following SO items:

    • C++ version
    • a better (though still not perfectly safe) C version

    Note: C99 VLAs (variable length arrays) might not follow all of these rules (in particular, they can be passed as parameters with the array size known by the called function). I have little experience with VLAs, and as far as I know they're not widely used. However, I do want to point out that the above discussion might apply differently to VLAs.

提交回复
热议问题