4 ms·
An array is not a pointer. These are completely different data types. For example, you can't apply pointer arithmetic to arrays without casting them to pointers
by napsy 6y ago
An array is not a pointer. These are completely different data types. For example, you can't apply pointer arithmetic to arrays without casting them to pointers.
- WalterBright 6y agoThat's right. They are converted to pointers when passed to a function, even if the function declares the parameter as an array.
- napsy 6y agoThey're not converted but can be implicitly casted to pointer types.
- _kst_ 6y agoNo, they're converted. There is no such thing as an "implicit cast". And it's not specific to arguments in function calls. Array types and pointer types are distinct. An expression of array type is, in most but not all contexts, implicitly converted (really more of a compile-time adjustment) to an expression of pointer type that yields the address of the 0th element of the array object. The exceptions are when the array expression is the operand of a unary & (address-of) or sizeof operator, or when it's a string literal in an initializer used to initialize an array (sub)object. (The N1570 draft incorrectly lists _Alignof as another exception. In fact, _Alignof can only take a parenthesized type name as its operand.) If you do: int arr[10]; some_func(arr); then arr is "converted" to the equivalent of &arr[0] -- not because it's an argument in a function call, but because it's not in one of the three contexts listed above in which the conversion doesn't take place. Another rule that causes confusion here is that if you define a function parameter with an array type, it's treated as a pointer parameter. For example, these declarations are exactly equivalent: void func(int arr[]); void func(int arr[42]); // the 42 is quietly ignored void func(int *arr); Suggested reading: http://www.c-faq.com/ http://www.c-faq.com/, particularly section 6, "Arrays and Pointers". A conversion converts a value of one type to another type (possibly the same one). The term "cast" refers only to an explicit conversion, one specified by a cast operator (a parenthesized type name preceding the expression to be converted, like "(double)42"). An implicit conversion is one that isn't specified by a cast operator.
- saagarjha 6y agoA little-known but useful C feature is static array indices, as in: void foo(int array[static 42]); which means you can't pass in an array of less than 42 elements (and the compiler can warn you if it notices you are).
- JoeAltmaier 6y agoSure you can. int aFoo[]; has many legal array operations possible: *(aFoo+3) should work fine and return the 4th int in the array.
- pvarangot 6y agoI think your star operator there is making the compiler cast your array to pointer implicitly.
- JoeAltmaier 6y agoIts all symbols, so you can say whatever. But if it looks like a pointer, walks like a pointer, and quacks like a pointer, Its A Pointer.
- quelsolaar 6y agothey are accessed using pointer arithmetic, if you wanted them to contain length data, you would need a different access pattern. I think one of the great features of C is that it doesn't do anything under the hood, its all explicit. If you want to bounds check, then do it.
- WalterBright 6y ago> they are accessed using pointer arithmetic Not always. Consider: int a[3]; a[1] = 2; This is not using pointer arithmetic. Dump the generated code if you don't believe me :-)
- quelsolaar 6y agoIts still pointer arithmetic, its just done compile time rather then at execution. Still, you deserve style points :-)
- saagarjha 6y agoTell that to mov DWORD PTR [rsp - 8], 2