This question already has answers here:
difference between the dot operator and arrow operator by structure object variable for tree creation in c or c++ [duplicate]
(3 answers)
Closed 7 years ago.
I' just got started in learning struct in c language.
i Thought "->" and "." were equivalent but i get the following error when using "->" instead of ".":
invalid type argument of '->' (have 'struct item')
a->b is short for (*a).b.
There is no difference between a->b and (*a).b. There is a difference between (*a).b and a.b, of course - which is that the * dereferences a first (which must be a pointer or an array).
Related
This question already has answers here:
Argument conversion: (normal) pointer to void pointer, cast needed?
(1 answer)
Concept of void pointer in C programming
(16 answers)
Closed 2 years ago.
The pthread_exit function should take a void pointer as input. I'm wondering how come it's possible to pass the address of an integer variable (e.g. pthread_exit(&ret1) here) without performing a cast conversion.
This question already has answers here:
What is array to pointer decay?
(11 answers)
Why can't we pass arrays to function by value?
(9 answers)
Difference between passing array and array pointer into function in C
(3 answers)
Closed 3 years ago.
In C, if you pass an array as an argument to a function, what actually gets passed? And Why?
See the standard.
On entry to the function, the size expressions of each variably modified parameter are
evaluated and the value of each argument expression is converted to the type of the
corresponding parameter as if by assignment. (Array expressions and function
designators as arguments were converted to pointers before the call.)
This question already has answers here:
Why does C have a distinction between -> and .?
(7 answers)
Closed 6 years ago.
What is the difference between -> and . while calling variables in a structure?I have seen both of them used in various scenarios but couldn't identify the difference between them.
-> means you have a variable that points to a piece of memory containing the struct. To access its members, you must dereference the pointer and then add the offset to the member. the -> does that for you.
the . means your variable is the structure and you only need to add the offset to the member.
As user Eliot B points out, if you have a pointer s to a struct, then accessing the member elem can be done in two ways: s->elem or (*s).elem.
With (*s) you have an expression that "is" the struct and you now use the dot-operator to access elem.
s->elem is equal to (*s).elem
https://en.wikipedia.org/wiki/Dereference_operator
The difference is about the structure's defined instance. '->' and '.' operators is always about the left operand.
If left operand is a pointer, then you use '->', else you use '.'.
For example.
struct Foo bar1;
struct Foo* bar2 = malloc(sizeof(struct Foo));
bar1.variable = "text";
bar2->variable = "text";
x->y (-> is the pointer to member operator) is equivalent to (*x).y. Because of operator precedence, you can't write *x.y as that would be evaluated as *(x.y).
The former is easier to type and is a lot clearer. It's used when x is a pointer to a structure containing the member y.
This question already has answers here:
Why do C and C++ support memberwise assignment of arrays within structs, but not generally?
(5 answers)
Closed 6 years ago.
An lvalue is defined as an expression to which a value can be assigned.
And it is illegal to assign and array with a array value. E.g.:
int x[2],y[2];
x = y;
Whereas structures can be treated as lvalues. Below structure assignment is valid.
typedef struct car {
char color[20];
int price;
} CAR;
CAR audi, bmw;
audi = bmw;
What is the difference?
There are historic reasons why arrays are not assignable on themselves, but are assignable inside structs. There is really no technical reason for this discrepancy.
Anecdottal heresay is that when C was designed, it was based on a certain language (don't remember which one!) which didn't have array assingment, so this feature was exluded from C as well - to preserve compatibility. However, this language didn't have structs, so array assingment inside structs was OKayed.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does the arrow (->) operator in C exist?
Why does C have both . and -> for addressing struct members?
Is it possible to have such modified language syntax, where we can take p as a pointer to struct and get a struct member's value just as p.value?
You can think of p->m as shorthand for (*p).m
From the C99 Spec.
The first operand of the . operator shall have a qualified or unqualified structure or union
type, and the second operand shall name a member of that type.
The first operand of the -> operator shall have type pointer to qualified or unqualified
structure or pointer to qualified or unqualified union, and the second operand shall
name a member of the type pointed to.
My guess is, for identification purpose they used two operators for member access. i.e for pointer type struct variable is -> and . for ordinary struct variable.
For example:
struct sample E, *E1;
the expression (&E)->MOS is the same as E.MOS and (*E1).MOS is the same as E1->MOS
Is it possible? Yes. The syntax is as follows:
(*ptr).member
The parentheses are required because the structure member operator . has higher precedence than the indirection operator *. But after using that a few times you will agree that the following is easier to use:
ptr->member
Why does C have both? Pointers to structures are used so often in C that a special operator was created, called the structure pointer operator ->. It's job is to more clearly and conveniently express pointers to structures.
. is for struct variable, and -> is for pointer. If p is a pointer, you can do p->value or (*p).value, they are same.