3 ms·
The pointer-to-array solution is okay, with the caveat that pointer-to-array typedefs should be avoided. The problem is that they are attractive for reducing r
by kazinator 11mo ago
The pointer-to-array solution is okay, with the caveat that pointer-to-array typedefs should be avoided.
The problem is that they are attractive for reducing repeated declarations:
typedef unsigned char thing_t[THING_SIZE];
struct red_box_with_a_hook {
thing_t thing1, thing2;
}
void shake_hands_with(thing_t *thing);
That is all well. But thing_t is an array type which still decays to pointer.
It looks as if thing_t can be passed by value, but since it is an array, it sneakily isn't passed by value:
void catch_with_net(thing_t thing); // thing's type is actually "usnsigned char *"
// ...
unsigned char x[42]];
catch_with_net(x); // pointer to first element passed; type checks
- uecker 11mo agoIndeed, I think one should avoid such typedefs. You could wrap it in a struct.
- dfawcus 11mo agoyet if one does that, and has suitable warnings turned on, GCC will complain: array2.c: In function ‘main’: array2.c:25:17: warning: passing argument 1 of ‘arr_fn2’ from incompatible pointer type [-Wincompatible-pointer-types] 25 | arr_fn2(array); | ^~~~~ | | | char * array2.c:13:22: note: expected ‘char (*)[15]’ but argument is of type ‘char *’ 13 | void arr_fn2(Arr_15 *arr) { | ~~~~~~~~^~~ The above was -Wall -Wextra Or are you just referring to the function where one defines it as apparently 'pass by value'?