Any functional difference between (* p) & # 8594; some_var and * p-> some_var

I saw several C code examples that use the (* p) - > some_var way of working with pointers (using parsers around the pointer).

My question is: is there any functional difference between the two methods of working with C pointers, and if so, how did this happen?

+4
source share
4 answers

Those do not do the same. For comparison:

(*p)->some_var // aka (*(*p)).some_var

This means that "p is the pointer that we dereference, and then look again to access the field."

*p->some_var // aka *((*p).some_var)

This means that "p is the pointer we are looking for to access the field, which is the pointer we are looking for."

+10

* , ->, *p->some_var *(p->some_var), , , (*p)->some_var.

some_var , p (p - some_struct *)

struct some_struct {
    int *some_var;
};

some_struct *p = ...;
int v = *p->some_var;

p some_var, ( p - - some_struct **).

some_struct *a = ...;
some_struct **p = &a;
int *val_ptr = (*p)->some_var;
+3

:

(*p)->some_var

, -:

( operator* identifier ) operator-> identifier 

, :

Precedence  Operator    Description     ...
         1  ... -> ...  Structure and union member access through pointer   
         2  ... * ...   Indirection (dereference)

, :

(operator-with-prec-2 identifier) operator-with-prec-1 identifier

? . *p->somevar, *(p->somevar) - . parens .

, , / . - , .

+3
source
(*p)->some_var

pis a pointer to a structure or association. It is dereferenced, and then its field opens some_var.


*p->some_var

pis a structure or association. His field is some_vardereferenced.

+2
source

Source: https://habr.com/ru/post/1684711/


All Articles