C Structure Accessing and Initialization

In the realm of C programming, mastering the manipulation of structures is a pivotal skill. Among the core concepts to grasp is the accessing of structure members, which enables the utilization of individual elements within a structure.

Accessing Structure Members

Just like conventional variables, individual members of a structure can be accessed directly. This is achieved through the use of the dot operator (.), also known as the structure member operator. Its syntax is straightforward:

structurevariable.member-name;

For instance, consider the following coordinate structure:

struct coordinate {
    int x;
    int y;
};

To assign coordinates to a structure named first, wherein x=50 and y=100, you would use:

first.x = 50;
first.y = 100;

Likewise, to display the coordinates stored in a structure named second, you could employ:

printf ("%d,%d", second.x, second.y);

It's crucial to note that structure members behave akin to regular data elements and can thus be accessed accordingly.

Illustrative Example

Let's delve into a program example to solidify our understanding. Consider the scenario of storing and retrieving values from a student structure:

#include<stdio.h>

struct student {
    int roll_no;
    char name[20];
    char course[20];
    int marks_obtained;
};

int main() {
    struct student s1;

    printf("Enter the student roll number: ");
    scanf("%d", &s1.roll_no);

    printf("\nEnter the student name: ");
    scanf("%s", s1.name);

    printf("\nEnter the student course: ");
    scanf("%s", s1.course);

    printf("Enter the student percentage: ");
    scanf("%d", &s1.marks_obtained);

    printf("\nData entry is complete.\n");
    printf("The data entered is as follows:\n");

    printf("\nThe student roll no is %d", s1.roll_no);
    printf("\nThe student name is %s", s1.name);
    printf("\nThe student course is %s", s1.course);
    printf("\nThe student percentage is %d\n", s1.marks_obtained);

    return 0;
}

Upon execution, the program prompts the user to input student details and then displays the entered information.

Output:

Enter the student roll number: 1234
Enter the student name: ARUN
Enter the student course: MCA
Enter the student percentage: 84

Data entry is complete.
The data entered is as follows:
The student roll no is 1234
The student name is ARUN
The student course is MCA
The student percentage is 84

Another method of accessing and storing values in structure members involves initializing them during the creation of an instance of the data type. This offers an alternative approach for managing data within structures.

Initialization of Structures in C Programming

In C programming, structures can be initialized upon declaration, similar to arrays and other variable types. This process involves specifying initial values for each member of the structure within braces, separated by commas. Let's explore this concept with examples and elucidate how it's done.

Initialization Syntax

When initializing a structure, the declaration is followed by an equal sign and a list of initialization values enclosed in braces. Consider the example below, where the mysale structure variable is initialized:

struct sale {
    char customer[20];
    char item[20];
    float amt;
} mysale = { "XYZ Industries", "toolskit", 600.00 };

Nested Structures Initialization

In structures containing other structures as members, initialization values are listed in order, corresponding to the order of members in the structure definition. This is demonstrated in the following example:

struct customer {
    char firm[20];
    char contact[25];
};

struct sale {
    struct customer buyer1;
    char item[20];
    float amt;
} mysale = {
    { "XYZ Industries", "Tyran Adams" },
    "toolskit",
    600.00
};

In this example:

  • mysale.buyer1.firm is initialized to "XYZ Industries".
  • mysale.buyer1.contact is initialized to "Tyran Adams".
  • mysale.item is initialized to "toolskit".
  • mysale.amount is initialized to 600.00.

Program Illustration

Let's delve into a program illustrating the initialization of structure members with predefined values:

#include<stdio.h>

struct telephone {
    int tele_no;
    int cust_code;
    char cust_name[20];
    char cust_address[40];
    int bill_amt;
};

int main() {
    struct telephone tele = { 2314345, 5463, "Ram", "New Delhi", 2435 };

    printf("The values are initialized in this program.\n");
    printf("The telephone number is %d\n", tele.tele_no);
    printf("The customer code is %d\n", tele.cust_code);
    printf("The customer name is %s\n", tele.cust_name);
    printf("The customer address is %s\n", tele.cust_address);
    printf("The bill amount is %d\n", tele.bill_amt);

    return 0;
}

Output:

The values are initialized in this program.
The telephone number is 2314345
The customer code is 5463
The customer name is Ram
The customer address is New Delhi
The bill amount is 2435

Initialization of structures in C programming provides a convenient way to set initial values for structure members, streamlining the process of managing and accessing structured data.


Next Post Previous Post
No Comment
Add Comment
comment url