deepak said:
hi folks
I have a pointer to a file. If i increment the pointer, where will it
point. I could not find the problem in the FAQs.
It's FAQ 4.4, which doesn't appear to be in the online version. In that
answer, Steve Summit says that "pointer arithmetic in C is always
automatically scaled by the size of the objects pointed to", which is a
very good way to put it.
It is easy to make the mistake of thinking that a so-called "file pointer"
points to a position in the file, so that incrementing it would make it
point to the next position, but such thinking is flawed. A file pointer
points to a FILE object, and it is the FILE object contains information
about the stream being read, written or updated, such as (for example) a
current position indicator.
++fp; is normally asking for trouble. I can think of circumstances where it
might be useful, but they are not overly common.
Example of incrementing fp:
FILE OutputArray[12 + 1] = {0};
FILE *fp = NULL;
if(OK == OpenAllMyFiles(OutputArray,
sizeof OutputArray / sizeof OutputArray[0]) - 1)
{
for(fp = OutputArray; fp != NULL; fp++)
{
DoSomethingWith(data, fp);
}
}