Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
113 views
in C Programming by (111k points)
How do you access members of a structure?

Please log in or register to answer this question.

1 Answer

0 votes
by (111k points)

To access members of a structure in C, you use the dot (.) operator. The dot operator allows you to access and manipulate individual members of a structure variable. 

Here's the syntax:

structure_variable.member_name
 

Let's say you have a structure named Person with members name, age, and height. To access these members, you would use the dot operator as follows:

struct Person person1;

person1.age = 25;            // Accessing and assigning a value to the 'age' member
strcpy(person1.name, "John Doe");    // Accessing and assigning a value to the 'name' member
person1.height = 1.75;       // Accessing and assigning a value to the 'height' member
 

In this example, we create a structure variable person1 of type Person. To assign a value to the age member, we use person1.age with the dot operator. Similarly, we use person1.name to assign a value to the name member, and person1.height to assign a value to the height member.

You can also read the values of structure members using the same dot operator. 

For example:

printf("Name: %s\n", person1.name);    // Accessing and printing the value of the 'name' member
printf("Age: %d\n", person1.age);      // Accessing and printing the value of the 'age' member
printf("Height: %.2f\n", person1.height);  // Accessing and printing the value of the 'height' member
 

In this case, we use person1.name, person1.age, and person1.height to access and print the values of the respective members.

Remember to use the dot operator (.) to access the specific member of a structure variable you want to read or modify.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...