之间的区别 ->和 。在一个结构中?

发布于 2024-11-07 03:59:17 字数 342 浏览 0 评论 0原文

如果我有一个像“那么”这样的结构,

struct account {
   int account_number;
};

什么区别

myAccount.account_number;

“doing”和

myAccount->account_number;

“or”之间有

?如果没有区别,为什么不直接使用 . 表示法而不是 ->-> 看起来很乱。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

甚是思念 2024-11-14 03:59:17

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

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

accountp->account_number;

(*accountp).account_number;

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

If you have a pointer to a struct, then saying

accountp->account_number;

is much more concise than

(*accountp).account_number;
终止放荡 2024-11-14 03:59:17

处理变量时可以使用 .。当你处理指针时,你可以使用->

例如:

struct account {
   int account_number;
};

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

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

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

使用 a->; account_number = 1;(*a).account_number = 1; 的替代语法,

我希望这会有所帮助。

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 s;
...
// initializing the variable
s.account_number = 1;

Declare a as a pointer to 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;

I hope this helps.

誰ツ都不明白 2024-11-14 03:59:17

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

// 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;

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;
箹锭⒈辈孓 2024-11-14 03:59:17

->是指针取消引用并且 .组合访问器

-> is a pointer dereference and . accessor combined

若相惜即相离 2024-11-14 03:59:17

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

myAccount->account_number;

如果不是,请改用以下语法:

myAccount.account_number;

If myAccount is a pointer, use this syntax:

myAccount->account_number;

If it's not, use this one instead:

myAccount.account_number;
灼痛 2024-11-14 03:59:17

是的,您可以两种方式使用结构成员...

一种是使用 DOt:(" . ")

myAccount.account_number;

另一种是:(" ->”)

(&myAccount)->account_number;

yes you can use struct membrs both the ways...

one is with DOt:(" . ")

myAccount.account_number;

anotherone is:(" -> ")

(&myAccount)->account_number;
‘画卷フ 2024-11-14 03:59:17

printf("书名:%s\n", book->subject);
printf("图书代码:%d\n", (*book).book_code);

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

七颜 2024-11-14 03:59:17

引自 K & R 第二版. :

"(*pp).x 中括号是必需的,因为结构体成员运算符 . 的优先级高于 *。表达式 *pp.x 表示 *(pp.x),这里是非法的,因为 x 不是 结构指针

的使用非常频繁,因此提供了一种替代符号作为简写。”

如果 pp 是指向结构的指针,则 pp->member-of-struct
指的是特定的成员。

Quote from K & R the second edition. :

"The parentheses are necessary in (*pp).x because the precedence of the structure member operator . is higher than *. The expression *pp.x means *(pp.x), which is illegal here because x is not a pointer.

Pointers to structures are so frequently used that an alternative notation is provided as a shorthand."

If pp is a pointer to a structure, then pp->member-of-structure
refers to the particular member.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文