[RESOLVED] Pointer Increment
To whom it may concern,
I just noticed something odd while looking at a piece of example code.
I believe I am misunderstanding an aspect of how C++ handles type casting and the incrementing of pointers.
Here's an excerpt from main():
Code:
int n[5] = {1, 2, 3, 4, 5};
ofstream out("test", ios::out | ios::binary);
out.write((char *) &n, sizeof n);
What I do not understand is how the write() function can output a four byte integer value ('n') after casting it to a one byte char pointer.
The declaration for the write() function is as follows:
ostream& write (const char* s , streamsize len);
To access the next value, presumably 's' would be incremented by one byte as any other char pointer. However, since each value of the 'n' array is four bytes long it would seem that 's' would now point to the second byte of the first item in the 'n' array. Yet, this program appears to function as if, after being incremented, 's' points to the second value in the 'n' array.
What am I missing?
Thanks for any clarification,
Ben