c - typecasting a pointer to an int . -
i can't understand output of program . of , , first of , pointers p, q ,r ,s pointing towards null .
then , there has been typecasting . how heck , did output come 1 4 4 8 . might wrong in thoughts . , please correct me if wrong .
int main() { int a, b, c, d; char* p = (char*)0; int *q = (int *)0; float* r = (float*)0; double* s = (double*)0; = (int)(p + 1); b = (int)(q + 1); c = (int)(r + 1); d = (int)(s + 1); printf("%d %d %d %d\n", a, b, c, d); _getch(); return 0; }
pointer arithmetic, in case adding integer value pointer value, advances pointer value in units of type points to. if have pointer 8-byte type, adding 1 pointer advance pointer 8 bytes.
pointer arithmetic valid if both original pointer , result of addition point elements of same array object, or past end of it.
the way c standard describes (n1570 6.5.6 paragraph 8):
when expression has integer type added or subtracted pointer, result has type of pointer operand. if pointer operand points element of array object, , array large enough, result points element offset original element such difference of subscripts of resulting , original array elements equals integer expression.
[...]
if both pointer operand , result point elements of same array object, or 1 past last element of array object, evaluation shall not produce overflow; otherwise, behavior undefined. if result points 1 past last element of array object, shall not used operand of unary*
operator evaluated.
a pointer past end of array valid, can't dereference it. single non-array object treated 1-element array.
your program has undefined behavior. add 1
null pointer. since null pointer doesn't point object, pointer arithmetic on undefined.
but compilers aren't required detect undefined behavior, , program probably treat null pointer valid pointer value, , perform arithmetic on in same way. if null pointer points address 0
(this not guaranteed, btw, it's common), adding 1
probably give pointer address n, n size in bytes of type points to.
you convert resulting pointer int
(which @ best implementation-defined, lose information if pointers bigger int
, , may yield trap representation) , print int
value. result, on systems, show sizes of char
, int
, float
, , double
, commonly 1, 4, 4, , 8 bytes, respectively.
your program's behavior undefined, way behaves on system typical , unsurprising.
here's program doesn't have undefined behavior illustrates same point:
#include <stdio.h> int main(void) { char c; int i; float f; double d; char *p = &c; int *q = &i; float *r = &f; double *s = &d; printf("char: %p --> %p\n", (void*)p, (void*)(p + 1)); printf("int: %p --> %p\n", (void*)q, (void*)(q + 1)); printf("float: %p --> %p\n", (void*)r, (void*)(r + 1)); printf("double: %p --> %p\n", (void*)s, (void*)(s + 1)); return 0; }
and output on system:
char: 0x7fffa67dc84f --> 0x7fffa67dc850 int: 0x7fffa67dc850 --> 0x7fffa67dc854 float: 0x7fffa67dc854 --> 0x7fffa67dc858 double: 0x7fffa67dc858 --> 0x7fffa67dc860
the output not clear program's output, if examine results closely can see adding 1 char*
advances 1 byte, int*
or float*
4 bytes, , double*
8 bytes. (other char
, definition has size of 1 bytes, these may vary on systems.)
note output of "%p"
format implementation-defined, , may or may not reflect kind of arithmetic relationship might expect. i've worked on systems (cray vector computers) incrementing char*
pointer update byte offset stored in high-order 3 bits of 64-bit word. on such system, output of program (and of yours) more difficult interpret unless know low-level details of how machine , compiler work.
but purposes, don't need know low-level details. what's important pointer arithmetic works it's described in c standard. knowing how it's done on bit level can useful debugging (that's pretty %p
for), not necessary writing correct code.
Comments
Post a Comment