简体   繁体   中英

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. 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.

Is there a preference of the use of . and -> in 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.

虽然上面的代码将在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.

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).

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.

struct abcd aABCDStruct;
struct abcd * aPointerToABCDStruct;

Dot (.) is used for the struct variable while Arrow/ indeirection (->) is used by pointer variable to 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. We can iterate to next variable of list using 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM