The below expressions are equivalent:?
(*x).y
x->y
Yes, Both are two different ways to access structure member y
.
(*x).y
operator is .
DOT that works with value variable Element selection by reference
. that the reason *
used. means x
is pointer to struct.
x->y
operator ->
is used called Element selection through pointer
. This work with pointer to struct. that is the reason *
not used this time.
Both works same.
Would it be fair to think of this operator simply as preprocessor macro defined as such:
#define (x)-> (*(x).)
No First it give an error: macro names must be identifiers. This error is because we can't have ->
operator as macro name.
a valid macro name can be:
Macro names should only consist of alphanumeric characters and underscores, i.e. 'a-z', 'A-Z', '0-9', and '_', and the first character should not be a digit. Some preprocessors also permit the dollar sign character '$', but you shouldn't use it; unfortunately I can't quote the C standard since I don't have a copy of it.
Also, note ->
and .
are differences operators as I state above. also their precedence are different so its bad idea to replace one operator with other.
Valid macros to access struct elements:
Additionally I would like to share today only i came to know that most C header files. Defined macros like:
#define S(x) ((x).y)
for specific strcut element.
Notice (x)
parenthesis around x
is to overwrite precedence of *
over .
. By default .
DOT has higher precedence over *
So that it can be use for pointer and simple variable. Below my example will be helpful I think.
#define S(x) ((x).y)
typedef struct {
int y;
}X;
X *x;
X b;
int main(){
S(*x);
S(b);
}
EDIT:
Better Option
I am extending my idea to access strcut elements, I defined new macro:
#define S(x,y) ((x).y)
typedef struct {
int a;
}X;
X *x;
X b;
int main(){
S(*x,a);
S(b,a);
}
Not its not more for specif elements via macros.
Hope at-least OP love it :)