문제

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 ..

  • If you have a struct foo myFoo, you should use myFoo. or (&myFoo)->.
  • If you have a 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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top