by smcameron · 5y ago · 0 comments
To really drive home the primitiveness of C arrays, should probably also mention that, because addition is commutative, you could also write
i[array]
and somewhat surprisingly, it will compile, and work, and it means "*(i + array)" which is equivalent to "*(array + i)"But nobody really does that, because that would be kind of insane.
putc(2["ABCDEF"], stdout);
This prints 'C'.int const& x; // C++
A reference is functionally equivalent to a const pointer. (Reference reassignment is disallowed. Likewise, you cannot reassign a const pointer. A const pointer is meant to keep its pointee [address].) The difference between them is that C++ const references also allow non-lvalue arguments (temporaries).
It is much easier to read from right to left when decoding types. Look for yourself:
- double (* const convert_to_deg)(double const x) // const pointer to function taking a const double and returning double
- int const (* ptr_to_arr)[42]; // pointer to array of 42 const ints
- int const * arr_of_ptrs[42]; // array of 42 pointers to const ints
- int fun_returning_array_of_ints()[42];
Try it out yourself: https://cdecl.org/
Hence, I am an "East conster". (Many people are "West consters" though.)
You can return function pointers:
typedef struct player_t player_t; // let it be opaque ;)
int game_strategy1(player_t const * const p)
{
/* Eliminate player */
return 666;
}int game_strategy2(player_t const * const p)
{
/* Follow player */
return 007;
}int (* const game_strategy(int const strategy_to_use))(player_t const * const p)
{
if (strategy_to_use == 0)
return &game_strategy1;
return &game_strategy2;
}Functional programming = immutable (const) values + pure functions (no side effects).
Consting for me is also a form of documentation/specification.
"East const" for life! :)
Just saying that "actually it's just array + i" makes more sense - for me(!).
To make it worse, it’s in a parser for external data in binary format, where you really shouldn’t be playing funny tricks.
Yeah. That's why people don't do things in C. It's more like most C programmers probably weren't aware of this. After your comment, we'll start to see C codebases everywhere with that.
If you want to manipulate memory directly - which is risky though sometimes useful - C is one of the best languages in which to do it. Memory addresses are numbers, and C will let you work with those numbers in whatever way you want: add, subtract, multiply, divide... and if you didn't shudder at the suggestion of dividing a pointer because there are very, very few reasons to do so then C is not the language for you!
If you don't want to manipulate memory directly, you probably shouldn't be using C; stick with a nice garbage-collected, type-safe, object-oriented, cross-platform language. If you do want to manipulate memory directly, but you want more guarantees on what you can do with pointers, try Rust.
"The array subscripting operation is defined so that E1[E2] is identical to *(E1+E2). Therefore, despite its asymmetrical appearance, subscripting is a commutative operation."
(my emphasis)
int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);} int i;
int main(){
for(
;
i["]<i;++i){--i;}"]; // loop until i == 14? (the NUL byte on the end)
read('-'-'-',i+++"hello, world!\n",'/'/'/') // read(0, <one byte of "hello, world!\n" at a time>, 1)
) {};
}
int read(j,i,p){
write(j/p+p,i---j,i/i); // write(0/1+1, i-- - 0, 1) --> write(1, i, 1) --> write a byte to STDOUT
}