As specified in the section How does incrementing a pointer work? I have the following question.
How does a pointer know the base size of the data it points to? Pointers have a base type size so they can know how to increase?
I expect the following code to move the pointer forward one byte:
int intarr[] = { ... };
int *intptr = intarr;
intptr = intptr + 1;
printf("intarr[1] = %d\n", *intptr);
In accordance with the accepted answer on a linked site with an increment of the pointer by bytes rather than the base sizeof, the pointed element will cause mass hysteria, confusion and chaos.
Although I understand that this is likely to be an inevitable result, I still don't understand how pointers work in this regard. It was not possible to declare a pointer voidfor some type array struct[], and if I did, how would the pointer voidknow to increment by sizeof(struct mytype)?
Edit: I believe that I have worked with most of the difficulties that I have, but I'm not quite there while this is being demonstrated in the code.
See here: http://codepad.org/0d8veP4K
#include <stdio.h>
int main(int argc, char *argv[])
{
int intarr[] = { 0, 5, 10 };
int *intptr = intarr;
printf("intptr(%p): %d\n", intptr, *intptr);
printf("intptr(%p): %d\n", intptr + 1, *(intptr + 1));
printf("intptr(%p): %d\n", intptr + 2, *(intptr + 2));
printf("intptr[0]: %p | intptr[1]: %p | difference: %d | expected: %d",
intptr, intptr + 1, (intptr + 1) - intptr, sizeof(int));
return 0;
}