c - How does the OS know how much to increment different pointers? -
with 32-bit os, know pointer size 4 bytes, sizeof(char*) 4 , sizeof(int*) 4, etc. know when increment char*, byte address (offset) changes sizeof(char); when increment int*, byte address changes sizeof(int).
my question is:
- how os know how increment byte address
sizeof(yourtype)?
the compiler knows how increment pointer of type yourtype * if knows size of yourtype, case if , if complete definition of yourtype known compiler @ point.
for example, if have:
struct yourtype *a; struct yourothertype *b; struct yourtype { int x; char y; }; then allowed this:
a++; but not allowed this:
b++; ..since struct yourtype complete type, struct yourothertype incomplete type.
the error given gcc line b++; is:
error: arithmetic on pointer incomplete type
Comments
Post a Comment