Accessing structure members in C involves using the dot (.) operator to refer to individual members of a structure variable. This allows you to read or modify the values of the structure members.

Defining a Structure

To access members of a structure, you must first define a structure and then declare a variable of that structure type:

struct Person { char name[50]; int age; float height; };

In this example, Person is a structure type containing three members: name, age, and height.

Declaring a Structure Variable

After defining the structure, you can declare a variable of that type:

struct Person person1;

Accessing Members

To access or modify the members of the structure, use the dot operator (.). The dot operator is used between the structure variable and the member name:

// Assigning values to the members strcpy(person1.name, "Alice"); person1.age = 25; person1.height = 5.7; // Accessing and printing values of the members printf("Name: %s\n", person1.name); // Outputs: Name: Alice printf("Age: %d\n", person1.age); // Outputs: Age: 25 printf("Height: %.1f\n", person1.height); // Outputs: Height: 5.7

In this example:

  • person1.name refers to the name member of the person1 structure.
  • person1.age and person1.height access the age and height members, respectively.

Example Program

Here's a full example of defining, initializing, and accessing structure members:

#include <stdio.h> #include <string.h> struct Person { char name[50]; int age; float height; }; int main() { struct Person person1; // Assigning values to the structure members strcpy(person1.name, "Bob"); person1.age = 30; person1.height = 6.1; // Accessing and printing structure members printf("Name: %s\n", person1.name); printf("Age: %d\n", person1.age); printf("Height: %.1f\n", person1.height); return 0; }

Key Points to Remember

  1. Dot Operator (.): Used to access individual members of a structure.

    • Syntax: structure_variable.member_name.
    • Example: person1.age = 25;
  2. Assignment and Access:

    • You can assign values to structure members using the dot operator.
    • You can access the values of members the same way.
  3. String Members:

    • For character arrays (strings), use functions like strcpy() to assign values since direct assignment (=) doesn’t work with arrays.

Example with Multiple Structure Variables

You can have multiple variables of the same structure type and access their members independently:

struct Person person1, person2; strcpy(person1.name, "Alice"); person1.age = 25; person1.height = 5.7; strcpy(person2.name, "John"); person2.age = 28; person2.height = 6.0; printf("Person 1: %s, Age: %d, Height: %.1f\n", person1.name, person1.age, person1.height); printf("Person 2: %s, Age: %d, Height: %.1f\n", person2.name, person2.age, person2.height);

Accessing Members via Pointers

If you have a pointer to a structure, you access its members using the arrow operator (->):

struct Person *ptr = &person1; ptr->age = 26; // Equivalent to (*ptr).age = 26 printf("Age: %d\n", ptr->age);

Summary

  • Use the dot (.) operator to access members of a structure variable.
  • Accessing members involves specifying the structure variable, followed by a dot, and then the member name.
  • Pointers to structures use the arrow operator (->) to access members.

Accessing structure members is straightforward, and understanding it is crucial for working with user-defined data types in C.