C
Christian Bau
Krishanu Debnath said:Richard said:Suman said:Santhosh wrote:
All the major contributors to clc have said that any approach to do
what OP asked without using sizeof is silly.
[...]
printf("%u\n", (char *)(ptr+1) - (char *)ptr);
Its not silly.Its plain UB.
Is it? Yes, if size_t is not an unsigned int, but that's hardly
relevant, is it? If we correct that, how is this UB?
printf("%lu\n", (unsigned long) ((char *)(ptr+1)-(char *)ptr));
Richard
What it has to do with size_t or unsigned type? Pointer subtraction
gives you value of type ptrdiff_t which is signed integer type.
If size_t is not unsigned int, then ptrdiff_t is not int, and then the
format %u is incorrect (it is not strictly correct for outputting int,
but it is ok for positive values as in this case).