简体   繁体   English

结构指针及其用法

[英]Structure pointer and its use

What is the meaning of this statement in C? 这个陈述在C中的含义是什么?

where fag is structure and its pointer is pat . fag是结构,指针是pat

fag *pat = &h.device[d];
d = (pat - &(h.device[0]))/(sizeof(fag)) ;
fag *pat = &h.device[d];

takes the address od the d th element of the said array. 获取所述数组的第d个元素的地址。

It can be used to ease the access to it. 它可以用来方便访问它。

If I do 如果我做

d = (pat - &(h.device[0]))/(sizeof(fag));

where I don't have access to the original d , I get the index of the given entry, in the said array. 在我无法访问原始d ,我得到了所述数组中给定条目的索引。

It takes the difference from "our" pointer to the original one. 它从“我们的”指针到原始指针的区别。

If I see it right, the division by sizeof fag is wrong - the difference is already in terms of fag size. 如果我看到它的权利,通过划分sizeof fag是错误的-不同的是已经在以下方面fag大小。 Besides, &(h.device[0]) is exactly the same as h.device , so 此外, &(h.device[0])h.device ,所以

d = pat - h.device;

should be the right thing. 应该是正确的事情。 ( Thank you, WhozCraig. ) 谢谢你,WhozCraig。

结构指针的一种可能用途可能是,如果要更改函数中结构的成员,则必须传递结构的地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为函数中的结构正确分配内存以返回其值以供以后使用以及如何将结构转换为指针? - How to correctly allocate memory for a structure in a function to return its value for later use and how to cast a structure into a pointer? 使用结构体的字段分配结构体的指针 - Assigning a pointer to the structure with pointer to its field 结构的指针声明是否会为其成员分配内存? - Will a pointer declaration to a structure allocate memory for its members? 什么时候应该在结构中使用指向结构的指针? - When should a pointer to a structure should be use in a structure or not? 使用重定义void指针指向匿名结构的指针? - Use of redefining void pointer to pointer to an anonymous structure? 如何将结构用作指针和函数中的结构 - How to use a structure as a pointer and as an structure in functions 如何使用指针来使用嵌套结构内的结构成员? - How to use a pointer to use the members of a structure inside a nested structure? 如何在C中的结构中的双指针中使用malloc() - how to use malloc () in Double Pointer in Structure in C 我可以将 typedef 结构与指针一起使用吗? - Can I use my typedef structure with pointer? 如何在AC结构中使用指向字符的指针? - How to use a pointer to character within a c structure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM