How to access the structure variables in C Programming

Follow by Email
Facebook0
Facebook
Google+
https://codingsec.net/2017/04/access-structure-variables-c-programming/

Accessing Structure Members

We can access and assign values to the members of a structure 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. The link between a member and a variable is established using the member operator ‘ . ‘ which is also known as dot operator or period operator. The syntax for accessing a structure member is as shown below:

Examples of accessing the members of the student structure are shown below:

 

Structure Initialization

Like any other data type, a structure variable can be initialized at compile time. An example of compile time initialization of the student structure is shown below:

In the above example: teja, 10A1, 26 and A are assigned to student1’s name, rollno, age and grade. Whereas raju, 10A2, 24, B are assigned to student2’s name, rollno, age and grade.

Note: Partial initialization of a structure variable is supported. For example we can assign values to name and rollno and leave out age and grade. In case of partial initialization, the default values for int type is zero. For float and double it is 0.0. For characters and strings it is ‘\0’.

An example for dynamic initialization is shown below:

 

Arrays of Structures

We use structures to describe the format of a number of related variables. For example, we want to store details of 100 textbooks it will become difficult to declare and maintain 100 variables and store the data. Instead, we can declare and array of structure variables as shown below:

In the above example, we are declaring an array book with 10 elements of the type textbook which is a structure.

 

Take your time to comment on this article.

Follow by Email
Facebook0
Facebook
Google+
https://codingsec.net/2017/04/access-structure-variables-c-programming/

Add a Comment

Your email address will not be published. Required fields are marked *

Like the article? please consider sharing it. Thank you

Advertisment ad adsense adlogger