Answer: We know that we can access structure members in two different ways, one using dot ‘.’ operator and other using arrow ‘->’ operator. Let’s differentiate between two, consider an example,
/* dot_access.c -- program accesses structure members using dot operator */ #include <stdio.h> /* structure declaration */ typedef struct student { char srollno[10]; char sclass[10]; char name[25]; char fname[25]; char mname[25]; char add[200]; }Student; void dot_access(Student const); /* prototype */ int main(void) { Student a = {"35M2K14", "cs", "Christine", "James", "Hayek", "Post Box 1234, Park Avenue, UK"}; printf("Student a Information:\n"); dot_access(a); /* entire 'a' is passed */ return 0; } /* entire Student 'a' is copied into Student 'stu' */ /* 'stu' is a variable of Student, not a pointer */ void dot_access(Student const stu) { /* Let's access members of 'a' using dot operator */ print("roll no.: %s\n", stu.srollno); printf("class: %s\n", stu.sclass); printf("name: %s\n", stu.name); printf("father's name: %s\n", stu.fname); printf("mother's name: %s\n", stu.mname); printf("And address: %s\n", stu.add); }
Output as below,
Student a Information: roll no.: 35M2K14 class: cs name: Christine father's name: James mother's name: Hayek And address: Post Box 1234, Park Avenue, UK
Let’s rewrite above program with access mechanism employed as arrow operator,
/* * arrow_access.c -- program accesses structure members using arrow operator */ #include <stdio.h> typedef struct student { char srollno[10]; char sclass[10]; char name[25]; char fname[25]; char mname[25]; char add[200]; }Student; void arrow_access(Student const *); /* prototype */ int main(void) { Student a = {"35M2K14", "cs", "Christine", "James", "Hayek", "Post Box 1234, Park Avenue, UK"}; printf("Student a Information:\n"); arrow_access(&a); return 0; } void arrow_access(Student const *stu) /* 'stu' is a pointer-to-Student */ { /* Let's access members of 'a' using arrow operator */ printf("roll no.: %s\n", stu->srollno); printf("class: %s\n", stu->sclass); printf("name: %s\n", stu->name); printf("father's name: %s\n", stu->fname); printf("mother's name: %s\n", stu->mname); printf("And address: %s\n", stu->add); }
Output as follows,
Student a Information: roll no.: 35M2K14 class: cs name: Christine father's name: James mother's name: Hayek And address: Post Box 1234, Park Avenue, UK
No difference in the result! But which access method is more efficient than other, dot or arrow? As we know from our previous knowledge on Pointers, pointers are more efficient. Let’s try to explore this analytically.
In dot access mechanism, we copied, lump sum 300 bytes, Student ‘a’ from calling function to function parameter. Function parameter ‘stu’ gets copy of Student ‘a’ and performs on this copy. Since, dot_access() only displays information of a Student and not manipulates this. So we used ‘const’ keyword with function parameter to prevent dot_access() function from modifying any information in ‘stu’ Student.
Now, it’s turn to discuss arrow method. Just pointer to Student ‘a’ i.e. &a is copied to the pointer-to-Student ‘*stu’ at called function arrow_access(). Just 8 bytes copied. And using this pointer, we accessed the original contents of Student ‘a’ and displayed information. How about if you have to process just few members of the Student ‘a’? Is passing entire Student ‘a’ more efficient than passing pointer to it? Of course, pointers are more efficient!
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Check C Books
- Watch Advanced C Programming Videos
- Check Computer Science Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs