Re:Tell HN: C Experts Panel – Ask us anything about C
by gbear605 on Tuesday April 14, 2020 @12:41PM
(#22868116)
Declaring it with a custom struct:
Smart declaration with custom type: (assume that they'll come up with a good syntax)
With the custom struct, it requires the number `4` to be typed twice manually, while in the second it only needs a single input.
[ Parent
| on HN ]
Re:Tell HN: C Experts Panel – Ask us anything about C
by fkeeal on Wednesday April 15, 2020 @06:35PM
(#22883930)
You actually never need to specify the size explicitly in C.
Here are some other ways to declare your struct without needing as much boiler plate per declaration.
#define MAKE_SMARTARRAY(_smartarr, _array) \
do {\
_smartarr.val = (_array);\
_smartarr.len = sizeof((_array))/sizeof((_array[0]));\
}while(0)
struct SmartArray
{
int *val;
int len;
};
int main()
{
int array[] = {0,0,0,0};
struct SmartArray arr = {.val = array,
.len = sizeof(array)/sizeof(array[0])};
struct SmartArray arr2;
struct SmartArray arr3;
MAKE_SMARTARRAY(arr2, ((int[]){5,6,7,8}));
MAKE_SMARTARRAY(arr3, array);
return 0;
}