Structure in C

Level : Advanced
Mentor: Shailendra Chauhan
Duration : 00:06:00

What is Structure in C?

In C, structure is a data type that is specified by the user. It combines multiple data types to store a specific type of record. It is similar to an array. The only distinction between arrays and structures is that arrays contain collections of comparable data types, but structures can store any type of data.

Declaration of Structure in C

Before we can use structure in our program, we must first declare it in C. When we declare a structure, we specify the member variables as well as their datatype. In C, we can declare the structure using the struct keyword.

Definition of Structure in C

To use structure in our program, we must define its instance. We can do this by declaring variables of the structure type. There are two approaches to defining structure:

  1. Structure Variable Declaration using a Structure Template.
  2. Structure Variable Declaration comes after Structure Template.

Accessing a member of Structure

To access a member of a structure in C, use the dot operator (.) followed by the member name. For example, if you have a structure called "person" with a member called "name", you can access it with "".

Structure Members Initialization in C

Structure members cannot be initialized using the declaration. We can initialize structure members in three different ways, as follows:

  1. Using the Assignment Operator.
  2. Using the Initializer List.
  3. Using the Designated Initializer List.

Copying and Comparing Structure Variables in C

The assignment operator (=) in C allows you to copy structural variables member-wise. When comparing structures, you normally compare their members using comparison operators (e.g., ==,!=) to ensure equality or inequality based on your defined requirements.

How are Structure Elements stored?

  • Structure elements are stored in memory in continuous blocks.
  • Variable size is equal to the total of member sizes.
  • Word boundaries were used to store the information.
  • In a 2-byte word boundary system, left-aligned sequentially.
  • Slack bytes in variables may contain undefined values.
  • Even if all individuals are equal, structures may not be comparable.
  • C does not support structure comparisons.

What is an Array of Structure?

A grouping of structures together to form a single unit is called an array of structures. Each entry in the array represents a unique instance of the structure, allowing you to store numerous related data sets together. This is useful for managing information on multiple items of the same type.

What is an Array within a Structure?

An array within a structure is a member variable of the structure itself, which is an array. This allows you to store groupings of related data types alongside other attributes in a single structure. 

What is Pointer to Structure?

A pointer to a structure is a variable that contains the structure's memory address rather than the structure itself. This enables you to indirectly access and manipulate the structure's data.

Function and Structure

  • Passing the individual members to functions.​
  • Passing whole structure to functions.​
  • Passing structure pointer to functions.​
  • Passing an array of structures to functions.

Passing Individual Members to Functions

  • Create a structure that includes member variables.
  • Declare arguments in the function that share the same data types as the members.
  • During the call, provide the function with the structure variable's individual member values.

Passing Whole Structure to Functions

  • Create a structure that includes member variables.
  • Declare a parameter containing the structure type within the function.
  • When making the call, pass the entire structure variable to the method.

Passing Structure Pointer to Functions

  • Create a structure using member variables.
  • Declare a parameter that has a pointer to the structure type (struct Point *).
  • Use the & (address-of) operator to pass the structure variable's address during the call.
  • The member variables can be accessed with the arrow operator (->).

Passing Array of Structures to Functions

  • Define an array of structures (for example, struct Point[MAX_POINTS]).
  • Declare an argument in the method using a structure type array (struct Point[]).
  • Pass the name of the structure array (which degrades to a pointer) to the function.
  • Within the method, you can access specific elements by utilizing array indexing.

typedef for Structures

The typedef keyword is used to create an alias for an existing data type. To define variables in structures, we must use both the struct keyword and the structure name. This can sometimes lead to increased code length and complexity. We can use typedef to define a new, shorter name for the structure.

What are Nested Structures in C?

The C programming language allows us to include one structure as a member of another. This process is known as nesting, and the structures produced are referred to as nested structures. There are two techniques to nest one structure inside another:

  1. Embedded Structure Nesting: In this approach, the nested structure is also specified within the parent structure.
  2. Separate Structure Nesting: This approach declares two structures individually, and then nests the member structure inside the parent structure.

What are Self-Referential Structures in C?

In C, self-referential structures are those that include references to the same type as themselves, that is, a member of the type pointer referring to the same structure type.

Structure Uses in C

  • Structures specify specialized data types for complicated items such as dates and complex integers.
  • Large datasets can be efficiently organized into fields.
  • Form the basis for data structures such as trees and linked lists.
  • Allow functions to return multiple values.

Limitations of Structure in C

  • Higher Memory Consumption: This is caused by structural padding.
  • No Data Hiding: C Structures do not allow data hiding. Structure members are accessible from any function, wherever within the structure's scope.
  • Functions inside Structure: Because C structures do not allow functions within them, we are unable to provide the corresponding functions.
  • Static Members: C. Structures cannot contain static members within their bodies.
  • Construction Creation in Structure: Structures in C do not have constructors.
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
Accept cookies & close this