+ 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?

17th Jan 2020, 12:12 AM
Antônio Gabriel Zeni Landim
Antônio Gabriel Zeni Landim - avatar
3 ответов
+ 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).
17th Jan 2020, 12:31 AM
Jnn
Jnn - avatar
+ 1
THAT'S PERFECT! It's exactly what I wanted to know.
17th Jan 2020, 12:34 AM
Antônio Gabriel Zeni Landim
Antônio Gabriel Zeni Landim - avatar
0
My mistake, Kitten, I was just writing it from the top of my head. Thanks for clarifying.
18th Jan 2020, 4:15 PM
Antônio Gabriel Zeni Landim
Antônio Gabriel Zeni Landim - avatar