Answer: A structure, generally, comprises of one or more different types of members. For example,
typedef struct NODE { struct NODE *link; int value; char add[50]; } Node;
Let’s declare variables of ‘Node’ structure below,
int main(void) { Node current; Node next = {¤t, 100, "Building No. 100, xyz Lane, Bangalore"}; /* Let's access members of 'next' */ printf("\'link\' field of \'next\' points at address %p\n", next.link); printf("\'value\' of \'next\' is: %d\n", next.value); printf("\'add\' field of \'next\' is: \"%s\"\n", next.add); }
Observe that we used dot ‘.’ operator to access the members of structure ‘next’. For example, ‘next.link’ is a pointer to ‘current’ ‘Node’ structure and gives address of ‘current’, ‘next.value’ is an integer gives 100, and so on. Observe the output below,
'link' of 'next' points at address 0x7ffff52463f0 'value' of 'next' is: 100 'add' field of 'next' is: "Building No. 100, xyz Lane, Bangalore"
What about if we need to pass ‘next’ ‘Node’ as a function argument. We can pass copy of ‘next’, instead, for example,
int main(void) { Node current; Node next = {¤t, 100, "Building No. 100, xyz Lane, Bangalore"}; disp(next); return 0; } void disp(Node new) { /* here we process on copy of 'next' 'Node' */ /* any changes made on the copy wouldn't affect the original data */ }
There’s a draw back associated with this approach and that is we, firstly, copied ‘next’ ‘Node’ from calling fun. into formal parameter ‘new’ ‘Node’ even if we needed to perform on just one member of structure. Further worse, any changes made to the copy wouldn’t reflect on original data. To fix this problem, we can return the modified copy back to calling function as,
int main(void) { next = disp(next); return 0; } Node disp(Node new) { /* here we process on copy of next */ /* any changes made on the copy wouldn't affect the original data */ /* to make changes reflect into calling function */ return new; }
This approach still not an efficient one as it requires copying Nodes two ways even if just one member is to be manipulated. If we could recall what we had done in case of arrays; we might approach the right trick! Yes! Correct! We need to pass pointer to ‘next’. Let’s see, how?
/* * struct_mem_using_arrow.c -- program shows use of arrow to access struct * members */ #include <stdio.h> typedef struct NODE { struct NODE *link; int value; char add[50]; } Node; void disp(Node *); int main(void) { Node current; Node next = {¤t, 100, "Building No. 100, xyz Lane, Bangalore"}; /* Let's access members of 'next' */ disp(&next); return 0; } void disp(Node *new) { printf("\'link\' points at address: %p\n", new->link); printf("\'value\' of 'next' is: %d\n", new->value); printf("\'add\' field of 'next' is: \"%s\"\n", new->add); }
Notice that we passed pointer to ‘next’ as an argument of function disp() which, as we know, should be received into pointer-to-Node in the function parameter which we declared as ‘new’. Then we used arrow operator ‘->’ to access members of ‘next’ Node. This way, we accessed and manipulated the original members of ‘next’. Note that if function doesn’t need to manipulate the members, use keyword ‘const’ to prevent function from accidentally modifying original data. For example,
void disp(const Node *new) { /* process the members */ }
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Practice Computer Science MCQs
- Practice BCA MCQs
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Check Computer Science Books