C语言 -> 和 . 在结构中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5998599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:39:05  来源:igfitidea点击:

Difference between -> and . in a struct?

cstruct

提问by Sam

If I have a struct like

如果我有一个像

struct account {
   int account_number;
};

Then what's the difference between doing

那么这样做有什么区别

myAccount.account_number;

and

myAccount->account_number;

or isn't there a difference?

还是没有区别?

If there's no difference, why wouldn't you just use the .notation rather than ->? ->seems so messy.

如果没有区别,为什么不使用.符号而不是->->看起来好乱

回答by rmk

-> is a shorthand for (*x).field, where xis a pointer to a variable of type struct account, and fieldis a field in the struct, such as account_number.

-> 是 的简写(*x).field,其中x是指向类型变量的指针struct accountfield是结构中的字段,例如account_number.

If you have a pointer to a struct, then saying

如果你有一个指向结构的指针,那么说

accountp->account_number;

is much more concise than

(*accountp).account_number;

回答by CRM

You use .when you're dealing with variables. You use ->when you are dealing with pointers.

您可以使用.,当你处理变量。您可以使用->,当你正在处理的指针。

For example:

例如:

struct account {
   int account_number;
};

Declare a new variable of type struct account:

声明一个新的类型变量struct account

struct account s;
...
// initializing the variable
s.account_number = 1;

Declare aas a pointer to struct account:

声明a为指向 的指针struct account

struct account *a;
...
// initializing the variable
a = &some_account;  // point the pointer to some_account
a->account_number = 1; // modifying the value of account_number

Using a->account_number = 1;is an alternate syntax for (*a).account_number = 1;

Usinga->account_number = 1;是另一种语法(*a).account_number = 1;

I hope this helps.

我希望这有帮助。

回答by Rob?

You use the different notation according to whether the left-hand side is a object or a pointer.

根据左侧是对象还是指针使用不同的表示法。

// correct:
struct account myAccount;
myAccount.account_number;

// also correct:
struct account* pMyAccount;
pMyAccount->account_number;

// also, also correct
(*pMyAccount).account_number;

// incorrect:
myAccount->account_number;
pMyAccount.account_number;

回答by Grady Player

-> is a pointer dereference and . accessor combined

-> 是一个指针解引用和 . 存取器组合

回答by Juan Pablo Califano

If myAccountis a pointer, use this syntax:

如果myAccount是指针,请使用以下语法:

myAccount->account_number;

If it's not, use this one instead:

如果不是,请改用这个:

myAccount.account_number;

回答by teja

yes you can use struct membrsboth the ways...

是的,您可以同时使用结构成员...

one is with DOt:(" .")

一个是 DOt:(" .")

myAccount.account_number;

anotherone is:(" ->")

另一个是:(" ->")

(&myAccount)->account_number;

回答by speCial

printf("Book title: %s\n", book->subject); printf("Book code: %d\n", (*book).book_code);

printf("Book title: %s\n", book->subject); printf("Book code: %d\n", (*book).book_code);