Accessing Structure Members in C – theteche.com

We can access and assign values to the accessing structure members in a number of ways. As mentioned earlier, the members themselves are not variables. They should be linked to the structure variables in order to make them meaningful members. For example, the word title, has no meaning whereas the phrase ‘title of book3’ has a meaning. The link between a member and a variable is established using the member operator ‘.’ which is also known as ‘do operator’ or ‘period operator’. For example, book1.price is the variable representing the price of book1 and can be treated like any other ordinary variable. Here is how we would assign values to the members of book1:

strcpy (book1.title, “BASIC”);
strcpy (book1.author, “Balagurusamy”);
book1.pages = 250;
book1.price = 120.50;

We can also scanf to give the values through the keyboard.

scanf(“%s\n”, book1.title);
scanf(“%d\n”, &book1.pages);

are valid input statements.

Define a structure type, struct personal that would contain person name, date of joining and salary. Using this structure, write a program to read this information for one person from the keyboard and print the same on the screen.

Structure definition along with the program is shown. The scanf and printf functions illustrate how the member operator '.' is used to link the structure members to the structure variables. The variable name with a period and member name is used like an ordinary variable.

Program
          struct personal 
          {
               char name[20];
               int day;
               char month[10];
               int year;
               float salary;
          };
          main()
          {
                struct personal person;

                printf('Input Value\n");
                scanf("%s %d %s %d %f",
                          person.name,
                          &person.day,
                          person.month,
                          &person.year,
                          &person.salary);
               printf("%s %d %s %d %f\n",
                          person.name,
                          person.day,
                          person.momth,
                          person.year,
                          person.salary);
          }
Output
          Input Values
          M.L.Goel 10 January 1945 4500
          M.L.Goel 10 January 1945 4500.00

Accessing Structure Members – Structure Initialization

Like any other data type, a structure variable can be initialized at compile time.

main()
{
struct
{
int weight;
float height;
}
student = {60, 180.75};
…..
…..
}

This assigns the value 60 to student. weight and 180.75 to student. height. There is a one to one correspondence between the members and their initializing values.

A lot of variation is possible in initializing a structure. The following statements initialize two structure variables. Here, it is essential it is essential to use a tag name.

main()
{
struct st_record
{
int weight;
int height;
};
struct st_record student1 = { 60, 180.75 };
struct st_record student2 = { 53, 170.60 };
…..
…..
}

Another method is initialize a structure variable outside the function as shown below:

struct st_record
{
int weight;
float height;
} student1 = {60, 180.75};
main()
{
struct st_record student2 = {53, 170.60};
…..
…..
}

C language does not permit the initialization of individual structure members within the template. The initialization must be done only in the declaration of the actual variables.

Note that the compile-time initialization of a structure variable must have the following elements:

  1. The keyword struct.
  2. The structure tag name.
  3. A terminating semicolon.
  4. The assignment operator =.
  5. A set of values for members of the structure variable, separated by commas and enclosed in braces.
  6. The name of the variable to be declared.

Rules for Initializing Structures

There are a few rules to keep in mind while initializing structure variables at compile time.

  1. We cannot initialize individual members inside the structure template.
  2. The order of values enclosed in braces must match the order of members in the structure definition.
  3. It is permitted to have a partial initialization. We can initialize only the first few members and leave the remaining blank. The uninitialized members should be only at the end of the list.
  4. The uninitialized members will be assigned default values as follows:
Read More Topics
Functions of Layer in the OSI Model
Nesting of Function in C
Software Testing and Quality Assurance
Importance of C and Programming in ANSI C

About the author

Santhakumar Raja

Hi, This blog is dedicated to students to stay update in the education industry. Motivates students to become better readers and writers.

View all posts

Leave a Reply