+ 1
A question about pointers in C
So, I'm almost finishing the course in C but I have a doubt about pointers: when first introduced, I thought that pointers should be equal to the variable adress (&x, for example), and the * acts de-referencing, sort of nullifies the &, meaning *pointer = x, but when opening files, for example, the lesson says it returns a pointer for the file adress, but during the lessons the code example declares both char *pointer = fopen(file, mode) and char *pointer and then pointer = fopen(file, mode). What's the difference?
3 Respuestas
+ 3
Its the same.
If you initialize the pointer while declaring it (char *p = fopen(file, mode); ) you can use the pointer p directly after that.
If you first declare the pointer (char * p; ) you still need to initialize it (p = fopen(file, mode);).
In thr 2. statement p is still a char pointer with type char*. The * belongs to the type for declaration not to the name of the pointer (p).
+ 1
THAT'S PERFECT! It's exactly what I wanted to know.
0
My mistake, Kitten, I was just writing it from the top of my head. Thanks for clarifying.