fopen / fgets using char* instead of FILE*, why does this work?
c
Solution
Both a `char*` and a `FILE*` simply store a memory address. C has fairly weak typing (Edit: this was a misunderstanding on my part, see comments below) so it lets you assign pointers without worrying about the type they point to.
`fopen` returns the address of a `FILE` object and you store that address somewhere (in your case it is in a `char*`). When you use the address in `fgets` it still has the address of the `FILE` object so everything will work as expected.
Problem
I noticed that I had used a `char*` variable instead of a `FILE*` variable in my code when using fopen and fgets, but my code works. I am wondering why this is? A section of my code is as follows. ``` ... char* filePath = ac->filepath; char* line = malloc(sizeof(char) * MAX_CHAR_PER_LINE) ; filePath = fopen(filePath, "r"); // we are assigning the result to a 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)) { ... ```