c - fopen / fgets using char* instead of FILE*, why does this work? -
i noticed had used char* variable instead of file* variable in code when using fopen , fgets, code works. wondering why is? section of code follows.
... char* filepath = ac->filepath; char* line = malloc(sizeof(char) * max_char_per_line) ; filepath = fopen(filepath, "r"); // assigning result char*, not file* if (filepath == null) { printf ("\n[%s:%d] - error opening file '%s'", __file__, __line__, filepath); printf ("\n\n"); exit (1); } while ((fgets(line, max_char_per_line, filepath) != null)) { ...
both char* , file* store memory address. c has weak typing (edit: misunderstanding on part, see comments below) lets assign pointers without worrying type point to.
fopen returns address of file object , store address somewhere (in case in char*). when use address in fgets still has address of file object work expected.
Comments
Post a Comment