user2383960
user2383960

Reputation: 13

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.

Upvotes: 1

Views: 495

Answers (5)

AnT stands with Russia
AnT stands with Russia

Reputation: 320391

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 (i.e. when you know that the array will always be known in advance) you can choose either based on your personal preferences.

Upvotes: 0

r_goyal
r_goyal

Reputation: 1147

although the above code will show an error in C (not in C++) coz struct tagname is used without keyword struct (e.g. in line population p1,*p1_ptr;).. it is purely your choice to use a pointer in C for struct or not.. using a pointer may be advantageous/helpful in some cases like if u want to return more than one value from a struct type function using a pointer it is possible.. but if u choose not to use a pointer u dont.. as simple as that... also the dot(.) operator and (->) operator are the same and dont have an order of preferrence.. the only difference is in their use....

Upvotes: 1

user2618142
user2618142

Reputation: 1065

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.

Upvotes: 0

Nemanja Boric
Nemanja Boric

Reputation: 22157

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.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726489

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 structs?

No, there is no preference: p->x is an alternative syntax for (*p).x, the compiler would produce the same cod for both constructs.

Upvotes: 3

Related Questions