简体   繁体   English

为什么在引用结构中的数组元素时我们使用指针而不是索引

[英]why do we use pointers instead of index when referring to array elements in structs

struct individual {
    int element[100];
    int rank;
} ;

struct population{
    individual ind[10];
    individual *ind_ptr;
} ;

population p1,*p1_ptr;

p1_ptr = &p1;
p1_ptr->ind_ptr = &(p1_ptr->ind[0]);
p1.ind[0].element[0] = 1;
p1_ptr->ind_ptr->element[0] = 1;

The final two statements express the same thing. 最后两个陈述表达了同样的事情。 I am wondering why do I have to use pointers? 我想知道为什么我必须使用指针? Is there any advantages of pointers to indexes? 索引指针有什么优点吗? Or is there preference of the use of “.” and "->" in structs? 或者是否喜欢在结构中使用“。”和“ - >”? Thank you very much. 非常感谢你。

I've edited my codes again, correcting the mistakes pointed out by akash_sinha13134 and Jonathan Leffler .And Thank you for mbratch's comment. 我再次编辑了我的代码,纠正了akash_sinha13134和Jonathan Leffler指出的错误。感谢mbratch的评论。 Thank you very much. 非常感谢你。

I am wondering why do I have to use pointers? 我想知道为什么我必须使用指针?

You do not have to, it is your choice. 你不必,这是你的选择。

Is there any advantages of pointers to indexes? 索引指针有什么优点吗?

There is little, if any, difference between them on the modern hardware. 现代硬件上它们之间几乎没有差别。 On older hardware, however, pointers offered some improvement, because you could save on CPU cycles doing pointer arithmetic on indexing. 但是,在较旧的硬件上,指针提供了一些改进,因为您可以节省CPU周期,在索引时执行指针算法。

Is there a preference of the use of . 是否优先使用. and -> in struct s? ->struct s?

No, there is no preference: p->x is an alternative syntax for (*p).x , the compiler would produce the same cod for both constructs. 不,没有偏好: p->x(*p).x的替代语法,编译器会为两个结构生成相同的cod。

虽然上面的代码将在C中显示错误(不是在C ++中),但是coz struct tagname在没有关键字struct情况下使用(例如在行population p1,*p1_ptr; )。纯粹你可以选择在C中使用指针来表示struct或者在某些情况下使用指针可能是有利的/有帮助的,例如,如果你想使用指针从结构类型函数返回多个值,那么它是可能的..但是如果你选择不使用指针你不...就这么简单... dot(.) operator(->) operator是相同的,没有一个优先顺序..唯一的区别在于它们的使用....

In this example, yes, both ind_ptr and ind are pointing to the same memory location, so you don't have to use pointers. 在这个例子中,是的, ind_ptrind都指向相同的内存位置,因此您不必使用指针。

However, if you want to use your ind_ptr to point to another array of individual , or to change array you are using in your population in the runtime, you need to use the pointer there, and this is where this approach would be useful (in your case, it is not, and that may confuse you). 但是,如果要使用ind_ptr指向另一个individual数组,或者要更改运行时在群体中使用的数组,则需要在那里使用指针,这就是这种方法有用的地方(在你的情况,它不是,这可能会让你感到困惑)。

s->f is really just a shortcut for (*s).f , where s is a pointer to the struct containing field f , so there is no difference there. s->f实际上只是(*s).f的快捷方式,其中s是指向包含字段f的结构的指针,因此没有区别。

struct abcd aABCDStruct;
struct abcd * aPointerToABCDStruct;

Dot (.) is used for the struct variable while Arrow/ indeirection (->) is used by pointer variable to struct. 点(。)用于struct变量,而Arrow / indeirection( - >)由指针变量用于struct。

Assuming you knew the above, 假设你知道上述情况,
there is no difference as such, its just that using pointer to the struct facilitates your smooth shift in case of data structures you are using, like linked list. 这样没有区别,只是使用指向struct指针有助于在你使用的数据结构的情况下平滑移动,比如链表。 We can iterate to next variable of list using ptr->next . 我们可以使用ptr->next迭代到列表的下一个变量。

Pointer is a self-sufficient entity: in order to access the target object all you have to do is dereference the pointer. 指针是一个自给自足的实体:为了访问目标对象,您所要做的就是取消引用指针。 Pointers implement direct absolute access. 指针实现直接绝对访问。

Index is not a self-sufficient entity: you cannot access anything knowing just an index. 索引不是一个自给自足的实体:只有索引才能访问任何东西。 In addition to the index you need to know the array to which this index should be applied. 除了索引之外,您还需要知道应该应用此索引的数组。 Without the array, index by itself is useless. 没有数组,索引本身就没用了。 So, indices implement relative access. 因此,索引实现相对访问。

Pointers and indices have their own pros and cons that stem from the above fundamental difference between the two. 指针和指数各有利弊,源于两者之间的上述根本区别。 But in situations where the two are interchangeable (ie when you know that the array will always be known in advance) you can choose either based on your personal preferences. 但是在两者可互换的情况下(即,当您知道阵列将始终事先知道时),您可以根据个人喜好选择。

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

相关问题 为什么我们使用零长度数组而不是指针? - Why do we use zero length array instead of pointers? 为什么我们使用带指针数组的静态? - Why do we use static with array of pointers? 为什么我们在指针算术和数组地址中使用(字节)而不是(位)? - Why do we use (bytes) instead of (bits) in pointers arithmetic and array's addresses? 当用C中的指针定义时,为什么我们使用`const`作为字符串? - Why do we use `const` for strings when defining with pointers in C? 使用指向结构的指针数组,还是仅使用结构数组? - Use an array of pointers to structs, or just an array of structs? 为什么我们在结构数组中使用点运算符 (.) 而不是箭头运算符 (->) 作为函数参数? - Why do we use the dot operator (.) instead of the arrow operator (->) in arrays of structs as function parameters? 为什么我们对对象使用指针而不是直接使用对象值? - Why do we use pointers for objects instead of just the object value directly? 为什么以及何时应该在C中使用指针到指针而不是简单的指针? - Why and when should we use pointer-to-pointer instead of simple pointers in C? 我们什么时候使用函数指针 - when do we use function pointers 指向结构数组中的连续元素的指针不具有统一的偏移量吗? - Pointers to consecutive elements in an array of structs not at uniform offsets?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM