How can you have array of structures in C Programming

Follow by Email
Facebook0
Facebook
Google+
https://codingsec.net/2017/04/can-array-structures-c-programming/

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.

We can use the keyword typedef to define a structure as follows:

The name student represents the structure definition associated with it and therefore can be used to declare structure variables as shown below:

 

Structures within Structure

In C, structures can be nested. A structure can be defined within in another structure. The members of the inner structure can be accessed using the variable of the outer structure as shown below:

An example for a nested structure is shown below:

In the above example, student is the outer structure and the inner structure name consists of three members: fname, mname and lname.

 

Structures and Functions

We know that the functions are the basic building blocks of a C program. So, it is natural for C language to support passing structures as parameters in functions. We pass a copy of the entire structure to the called function. Since the function is working on a copy of the structure, any changes to structure members within the function are not reflected in the original structure. The syntax for passing the structure variable as a parameter is shown below:

 

Follow by Email
Facebook0
Facebook
Google+
https://codingsec.net/2017/04/can-array-structures-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