5 ms·
Arrays are pointers. If they aren't pointers then you need to copy the data when you are giving an array as a function parameter. that's a lot slower. Being abl
by quelsolaar 7y ago
Arrays are pointers. If they aren't pointers then you need to copy the data when you are giving an array as a function parameter. that's a lot slower. Being able to prepare an set of data in an array and then giving a pointer to a function is very useful. You could add a second type of array on top of what you have in C that includes more stuff, but if that's what you want you can implement that yourself with a struct.
- napsy 7y agoAn 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 7y agoThat's right. They are converted to pointers when passed to a function, even if the function declares the parameter as an array.
- napsy 7y agoThey're not converted but can be implicitly casted to pointer types.
- _kst_ 7y 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 7y 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 7y 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 7y agoI think your star operator there is making the compiler cast your array to pointer implicitly.
- JoeAltmaier 7y 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 7y 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 7y 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 7y agoIts still pointer arithmetic, its just done compile time rather then at execution. Still, you deserve style points :-)
- saagarjha 7y agoTell that to mov DWORD PTR [rsp - 8], 2