Part 1:
- Define Structures
- Accessing of structure variable in C language
- Passing structs to functions
- By Value
- By Reference
- Return struct from a function
- By Value
- By Reference
- Nested Structure in C
- Array in Struct
- Pointer structure
- Bit-Field in C
- An array of structures
- Pass to function by Value
- Pass to function by Reference
- Return from function
- Why use an array of structures?
- struct member is pointer
- Pointer structure
- Pointer to structure array
- 2-D structure (Array of struct has array member or struct array declared as array)
Part 3:
- struct member is function pointer
- Struct member is function pointer array
- Return value is
- Struct member is function pointer
- Struct member is function pointer array
- Self-reference structure
- Structure memory allocation
- Structure padding
- Use of struct and union in embedded field
====================================================================================================
C Struct
Structure is a user-defined data type in C/C++. Structure contains the heterogenous data. which allows the programmer to combine different types of data.
Difference between Array and structure is Array holds similar datatype of data but structure holds any datatype of data like int, char, boolean, float.
Struct can be defined without typedef and with typedef. we will see the advantage of typedef in later examples.
There are two ways to access structure members:
- By . (member or dot operator)
- By -> (structure pointer operator)
Without typedef struct
You look into the above example we done the following activities
- Define the structure by using keyword "struct" and declared the structure members.
- In the main function declared structure variable "b1" and initialized each member.
- Declared structure variable "b2" and initialized entire structure (Declaration and initialization)
- we are accessing the struct members by (.) dot operator.
Note: we are not used typedef keyword while defining the structure hence while declaring the struct variable we used entire struct like "struct book".
With typedef
Basically typedef keyword to allow users to provide alternative names for the primitive (e.g., int) and user-defined data types (e.g. struct).
Note that there is no longer a need to type struct again and again with every declaration of the variable of structure type.Passing structs to functions
You look into the above example we done the following activities
- Define the structure by using keyword "struct" and declared the structure members.
- we define the "display" function with argument type is struct "book"
- In the main function,
- we declared struct variable b1 and Initialized.
- we called display() function and passed struct variable b1
- once function is called struct elements are copied from "b1" to "x".
- we are accessing the struct members by using (.) dot operator.
Passing struct by reference
- Display function argument type is "struct pointer"
- while calling the function passed address of struct variable
- used arrow (->) operator for accessing the structure members
Return struct from a function by Value
You look into the above example we done the following activities
- Define the structure by using keyword "struct" and declared the structure members.
- We define the "display" function with return type is struct "book"
- In the main function,
- We declared struct variable b1
- We called display function
- The returned structure is displayed from the main() function
- We are accessing the struct members by using (.) dot operator.
- Once the function is called, struct variable x is declared and initialized
- struct variable "x" is returned.
Return struct from a function by Reference
Method 1: Access through global variable
this method same as method 1 but if any local calculations are required then modify the local struct variable and finally update to global struct variable.
Note: when returning any variable from function we should declare global variable. we should remember the scope of the variable if you tries to return local variable then we will get "Null value".
Structure within structure nothing but "Nested structure". Structure member is structure again is called "Nested structure"
- Struct within struct member is
- normal struct variable
- pointer struct variable
Define structure:
Method-2 is more efficient and readable because structs can be create in different header files and include them according to our requirement.
Access struct members (Method-1)
Struct within struct member is pointer struct variable
- first we defined "allowance" struct
- defined "Emp" struct with struct members Emp_name, Emp_nu and "allowance" struct of pointer type.
- In the main() function
- Initialized "allowance" struct with struct variable name "pstruct". pstruct is holds the struct member values.
- pstruct address is assigned to Emp struct variable "E1" along with other members.
- accessing the member with dot operator for normal variable and arrow operator for pointer variable
Array in Struct
Array member of struct is update and access in the main() function
struct member is passed to display() function as value from main() function. Access the struct member using dot operator in the display() function.
Array member of struct is passed to function as Reference
struct member is passed to display() function as Reference from main() function. Access the struct member using arrow operator in the display() function.
Pointer structure is similar like pointer variable but pointer variable holds the structure address.
- Created pointer struct "ptrbook", struct type "book".
- Assigned "b_sample" address to the "ptrbook"
- Accessing the struct members by using arrow operator.
Struct Bit-Fields in C
A bit field is a data structure that allows the programmer to allocate memory to structures and unions in bits in order to utilize computer memory in an efficient manner.
for example define structure to display time like HH:MM:SS
typedef struct{
unsigned int hours;
unsigned int minutes;
unsigned int seconds;
}without_BitField;
Hours range is 0 to 23 so it can be represent in 5 bits.
Minutes rage is 0 to 59 so it can be represent in 6 bits.
seconds rage is 0 to 59 so it can be represent in 6 bits.
typedef struct{
unsigned int hours:5;
unsigned int minutes:6;
unsigned int seconds:6;
}with_BitField;
compare the size of structure is :
- size of structure without bit filed is 12 bytes
- size of structure with bit filed is 4 bytes
Program as follows:
Advantages of Structure in C Programming
- Heterogeneous collection of data items
- Reduced complexity
- Increased productivity
- Maintainability of code
- Enhanced code readability
- Suitable for some mathematical operations
========================================================================================================
Topics like an array of structure, structure array pass to function as argument , return structure from function will discuss in the next article
========================================================================================================
Thanks for visiting the Post..!!
========================================================================================================
No comments:
Post a Comment