How to Access Members of a Structure in C?

Question: In What Different ways can we Access Members of a Structure in C Language?

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 = {&current, 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,

advertisement
advertisement
'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 = {&current, 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,

Note: Join free Sanfoundry classes at Telegram or Youtube
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?

advertisement
/*
 * 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 = {&current, 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.

advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.