Reputation: 13
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
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
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
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
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
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->
instruct
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.
Upvotes: 3