+ 1

How do pointers and file pointers differ?

11th Mar 2018, 12:10 PM
Abcdefg
3 Answers
+ 2
In very simple way to understand,pointers are variables which contain address of another variable. Suppose consider array,pointer, int a[3]={1,2,3}; int *p=a; printf("%d",*(a+2)); printf("%d",*p+2); Now,we know that a[2]=3 from above. So it can also be written as pointer to offset of array *(a+2) which is a pointer to base address of "a" with Source index of "2" and hence the value 3 is deferenced by *. So, Offset=(Base address + Source index) also called Load effective address to EBX register.And that is what exactly pointer *p+2 will point and dereference and hence value is 3.So both the same thing. Now on other hand we have file pointers(fp) which are structure pointers of type FILE. The structure FILE can be written as struct _IO_FILE. Syntax Declared as, FILE *fp; or FILE *<file_pointer_name>; Consider this example of pointers and file pointers in very simple way, FILE *fp; int **p; p=&fp; printf("%p",p); printf("\n%p",&fp); p=fopen("hello.dat","ab"); fclose(p); [Continued next page]
23rd Jun 2018, 9:08 PM
D-Key
D-Key - avatar
+ 2
Now you can see that file pointers can be manipulated using any pointers. But file pointers cannot be compared to any other pointers though comparision of pointers of different data types is totally legal in C.file pointer is of FILE type. This is because pointers are only comparable when they are of same type or homogeneous elements. Here File Pointer is of heterogeneous member elements or it has various different member to I/O var of different type in a single structure of struct _IO_FILE in FILE.Hence it differs from normal pointers in pointer comparisons of different type. File pointers opens and closes stream bytes of data from I/O to/from buffer.Unix has unbuffered file system. So fp defines the amount of memory block to be displaced or memory bytes of data associated to stream of files. Hence file handling functions like fseek(),ftell,fclose,fopen,fread,fwrite are all implications to memory bytes in form of data blocks by file pointers. [Continued next page]
23rd Jun 2018, 9:35 PM
D-Key
D-Key - avatar
+ 2
When file pointer is null an exception to Null is handled by the handler through the virtual table by routing it to memory process. The memory generates segmentation fault as you cannot access 0 process id.So null exception generates error and it's defined and hence the compilation error. So file pointers can be manipulated just like pointers but they cannot be compared like normal pointers do. file handling functions are defined for the members of file type of fp manipulation. In all the above mentioned ways FILE POINTER DIFFERS FROM POINTERS. While(1) hope clarified!!
23rd Jun 2018, 9:54 PM
D-Key
D-Key - avatar