Please read some good C programming books, they explain what are pointers and arrays.
A string literal like "file.txt"
is a char[]
array (but you should think of it as const char[]
because assigning inside string literals like "abc"[1]='D';
is undefined behavior with bad taste, and gcc -Wall
would warn you). Arrays can be (and usually are) understood as the pointer to their first cell (of index 0) - in other words arrays are decayed into pointers- So you can assign a string literal to a char
pointer.
However, arrays are not pointers in C; for instance the sizeof
some array is a suitable multiple of the size of each element, in particular sizeof("abcde") == 6
(because of the terminating null byte in every string literal). But the sizeof
some pointer is independent of the size of the pointed zone, and on most systems all pointers have the same size, the size of the machine word.
You need to explicitly ask the compiler for all warnings (eg gcc -Wall
on Linux) to get them.