Completing C code with D style
kdevel
kdevel at vogtner.de
Thu Nov 11 23:41:48 UTC 2021
On Thursday, 11 November 2021 at 22:30:12 UTC, forkit wrote:
[...]
> for(int i = 0; i < sizeof(numbers) / sizeof(int); ++i) //
> is so much safer - in C style
I made it even safer:
for (int i = 0; i < sizeof numbers / sizeof *numbers; ++i)
Maybe the type of numbers is changed in the future. sizeof is an
operator which needs parentheses around its argument only for
types. sizeof (non-type-arg) alleges a binding that does not
exist:
#include <stdio.h>
struct bar {
long l1;
long l2;
};
#define S(a) #a, (long unsigned) a
int main ()
{
struct bar *b;
printf ("%25s = %lu\n", S(sizeof(*b)));
printf ("%25s = %lu\n", S(sizeof(*b).l1));
printf ("%25s = %lu\n", S(sizeof(b)->l1));
return 0;
}
compiles (!) and gives:
sizeof(*b) = 16
sizeof(*b).l1 = 8
sizeof(b)->l1 = 8
More information about the Digitalmars-d-learn
mailing list