C中结构体名前加“*”是什么地址

发布于 2025-01-17 04:33:23 字数 470 浏览 6 评论 0原文

typedef struct PeoInfo
{
    char name[20];
    int age; 
    char sex[5]; 
    char tele[12]; 
    char addr[30];
}
struct PeoInfo c[10] = {0};
printf("p=%x\n", *c);
printf("p=%x\n", c[0]);

在这种情况下,sizeof(c)sizeof(*c) 会产生不同的结果。 在我看来,c 是数组的条目。但是*c 是什么? sizeof(*c) 的结果是 PeoInfo 的大小。使用printf打印*cc[0]的地址时,结果也不同。 我对此真的很困惑。

typedef struct PeoInfo
{
    char name[20];
    int age; 
    char sex[5]; 
    char tele[12]; 
    char addr[30];
}
struct PeoInfo c[10] = {0};
printf("p=%x\n", *c);
printf("p=%x\n", c[0]);

In this situation, sizeof(c) and sizeof(*c) have different results.
In my point of view, c is the entry of the array. But what is the *c? The result of sizeof(*c) is the size of PeoInfo. When using printf to print the address of *c and c[0], the result is also different.
I am really confused about it.

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

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

发布评论

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

评论(1

守不住的情 2025-01-24 04:33:23

在此表达式 *c 中,数组指示符 c 隐式转换为指向 struct PeoInfo * 类型的第一个元素的指针,并应用取消引用运算符生成该类型的对象结构 PeoInfo

因此,表达式 sizeof( *c ) (其中表达式 *c 未求值,仅确定其类型)会产生 struct PeoInfo 类型的对象的大小

由于下标运算符 c[0] 的计算方式与 *( c + 0 ) 相同,因此这些 printf 语句将输出相同的值

printf( "&*c = %p\n", ( void * )&*c );

printf( "&c[0] = %p\n", ( void * )&c[0] );

In this expression *c the array designator c is implicitly converted to a pointer to its first element of the type struct PeoInfo * and applying the dereference operator yields an object of the type struct PeoInfo.

So the expression sizeof( *c ) where the expression *c is not evaluated and only its type is determinated yields the size of an object of the type struct PeoInfo.

As the subscript operator c[0] is calculated like *( c + 0 ) that is the same as *c then these printf statements will output the same value

printf( "&*c = %p\n", ( void * )&*c );

printf( "&c[0] = %p\n", ( void * )&c[0] );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文