C language content

Accessing structure members in C involves understanding how structures are defined, how their members are accessed, and the various ways to manipulate these members. Here’s a detailed explanation:

1. Defining a Structure

A structure in C is a user-defined data type that allows grouping of variables of different types under a single name. It is defined using the struct keyword.

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

				
			

In this example, a structure named ‘Person‘ is defined with three members: ‘name‘, ‘age‘, and ‘height‘.

2. Declaring Structure Variables

Once a structure is defined, you can declare variables of that structure type.

				
					struct Person person1, person2;

				
			

Alternatively, you can declare and define a structure variable in a single statement.

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

				
			

3. Accessing Structure Members

Using Dot Operator

To access members of a structure variable, the dot operator . is used.

				
					person1.age = 25;
person1.height = 5.9;
strcpy(person1.name, "John Doe");

				
			

In this example:

  • person1.age sets the age member of person1 to 25.
  • person1.height sets the height member of person1 to 5.9.
  • strcpy(person1.name, "John Doe") copies the string “John Doe” into the name member of person1.

Using Pointer to Structure

When dealing with pointers to structures, the arrow operator -> is used to access the members.

				
					struct Person *ptr = &person1;
ptr->age = 30;
ptr->height = 6.1;
strcpy(ptr->name, "Jane Doe");

				
			

In this example:

  • ptr->age sets the age member of the structure pointed to by ptr to 30.
  • ptr->height sets the height member of the structure pointed to by ptr to 6.1.
  • strcpy(ptr->name, "Jane Doe") copies the string “Jane Doe” into the name member of the structure pointed to by ptr.

4. Initializing Structure Members

You can initialize structure members at the time of declaration.

				
					struct Person person3 = {"Alice", 28, 5.5};

				
			

In this example:

  • person3.name is initialized to “Alice”.
  • person3.age is initialized to 28.
  • person3.height is initialized to 5.5.

5. Nested Structures

Structures can also be nested within other structures.

				
					struct Address {
    char street[100];
    char city[50];
    int zipcode;
};

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

struct Person person4 = {"Bob", 45, 6.0, {"123 Main St", "New York", 10001}};

				
			

In this example, struct Address is nested within struct Person, and person4 is initialized with both Person and Address details.

6. Arrays of Structures

You can create arrays of structures to handle multiple records.

				
					struct Person people[3] = {
    {"Charlie", 30, 5.8},
    {"Diana", 27, 5.6},
    {"Edward", 35, 6.2}
};

				
			

In this example, struct Address is nested within struct Person, and person4 is initialized with both Person and Address details.

7. Functions and Structures

Structures can be passed to functions by value or by reference (pointer).

By Value

				
					void printPerson(struct Person p) {
    printf("Name: %s\n", p.name);
    printf("Age: %d\n", p.age);
    printf("Height: %.1f\n", p.height);
}

				
			

Call the function with a structure variable:

				
					printPerson(person1);

				
			

By Reference

				
					void updateAge(struct Person *p, int newAge) {
    p->age = newAge;
}

updateAge(&person1, 40);

				
			

In this example, updateAge updates the age member of person1 to 40.

Summary

  • Structures in C allow grouping of different data types under a single name.
  • Members of structures can be accessed using the dot operator for structure variables and the arrow operator for pointers to structures.
  • Structures can be initialized, nested, and passed to functions either by value or by reference.
  • Arrays of structures allow handling multiple records efficiently.

Understanding these concepts is fundamental to effectively using structures in C programming.

Scroll to Top