Left of ->
should be pointer type, while normal variables/instances for .
.
- If you have a
struct foo myFoo
, you should usemyFoo.
or(&myFoo)->
. - If you have a
struct foo *myFoo
, you should usemyFoo->
or(*myFoo).
.
Вопрос
In a C program, when i define a struct.
under which circumstances would i use "->" and under which "." ?
for example
typedef struct foo foo;
struct foo{
double bar;
double bar2;
}
foo barbar;
when would I use barbar.bar
and when would i use barbar->bar
?
Решение
Left of ->
should be pointer type, while normal variables/instances for .
.
struct foo myFoo
, you should use myFoo.
or (&myFoo)->
.struct foo *myFoo
, you should use myFoo->
or (*myFoo).
.Другие советы
Use a ->
for a pointer to a struct as it dereferences the pointer, use a .
for a struct