gpt4 book ai didi

c - 这两个陈述有什么区别?

转载 作者:太空宇宙 更新时间:2023-11-04 05:32:15 24 4
gpt4 key购买 nike

我写了下面的代码,有一些不同,但我想不通。

typedef struct {
char* ch;
int length;
}Hstring;
int Strlength(Hstring* s) {
return s->length; // if I use s.length there, error occurs.
}
int Strlength(Hstring s) {
return s.length; // if I use s->length there, error occurs.
}

那么这两种类型有什么区别呢?我会得到相同的结果吗?为什么会出现这些错误?

最佳答案

要添加到前面的答案中,点 (.) 用于“普通”变量,箭头 (->) 用于指针,请注意箭头是语法等同于指针取消引用后跟一个点,为方便起见(因为它是如此常见的操作)。

Hstring* s;
s->length; // this is equivalent to...
(*s).length; // ...this

括号是必需的,因为点的优先级高于星号。没有它们,您将 a) 使用带指针的点和 b) 尝试取消引用整数长度字段,这两者都是无效的。

Hstring* s;
*s.length; // this is equivalent to...
*(s.length); // ...this (not what you want at all)

关于c - 这两个陈述有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58536049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com