In this article we will discuss only Part-3 of structure concepts as mentioned below
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-referential structure in C
- Structure memory allocation
- Structure padding
- use of struct and union in embedded field.
Struct member is function pointer
we will start this topic with basic declarations
int x; /*variable "x " is Integer type*/
int x[10]; /*variable "x" is an array of 10 integers*/
int *ptrvar; /*ptrvar is pointer to Integer*/
int* ptr_array[10]; /*ptr_array is pointer that can point to an array of 10 integers*/
int func_add(int, init); /*func_add is a function of two integer arguments and return type is int*/
int func_add(init*, init*); /*func_add is a function of two pointer integer arguments and return type is int*/
int* func_add(init, init); *func_add is a function of two integer arguments and returns integer pointer*/
int* func_add(init*, init*); /*func_add is a function of two pointer integer arguments and returns integer pointer*/
int (*func_add)(int, int); /*func_add is a function pointer of two integers and returns integer*/
int (*func_add)(int*, int*); /*func_add is a function pointer of two pointer integers and returns integer*/
int* (*func_add)(int, int); /*func_add is a function pointer of two integers and returns pointer integer*/
int* (*func_add)(int*, int*); /*func_add is a function pointer of two pointer integers and returns pointer integer*/
int (*func_add[5])(int, int); /*func_add is an array of function pointer of two integers and returns integer*/
int (*func_add)(int, int)[5]; /*func_add is a function pointer of two integers and returns 5 integers*/
int* (*func_add)(int, int)[5]; /*func_add is a function pointer of two integers and returns pointer of 5 integers*/
Note: typedef is used for complex declaration. typedef will increase the code readability.
we will see programs of function pointer and an array of function pointer
Program of function pointer:
Be careful about declare a function pointer. place the function name and pointer within () bracket.
return type * func_name(arguments); this is not a function pointer. it is function return type is pointer.
return type (* func_name)(arguments); This is function pointer. please observe the placement of () bracket.
Program of "Array of function pointer"
return type (* func_name[array size])(arguments); this is an array of function pointer.
return type (* func_name)(arguments)[array size]; this is not an array of function pointer. it is function pointer returns the array.
Program of function pointer using typedef:
Please refer program of Program of function pointer, In that program we are not used typedef hence we are assigned one function address to the function pointer.
In the below program we created multiple function pointer and assigned different function.
typedef int (*func_op)(int, int); This will create named function for pointer hence we created and assigned different function.
func_op ptr1=addition;
func_op ptr2=subtraction;
func_op ptr3=multiplication;
As we discussed already about typedef, we can use a typedef to create a named function pointer type called "function name".
example typedef int (*func_op[3])(int, int); here function pointer type called "func_op".
func_op op; it is create array of 3 operation.
In the previous program we created separate variable for each operation now it is easy "op" variable can hold all three operations because it is an array.
op[0]= addition;
op[1] = subtraction;
op[2] = multiplication;
Note that don't create like func_op op[3]; this is because your typedef already an array so again it will create an array. internally look like this "func_op[3][3]" so it is 2-D array hence while accessing struct members it will through an error.
Now we will start the main topic "struct member is function pointer"
In this topic we will create function pointer as struct member and access the struct member as we already discussed in Part-2 (link : Struct concepts)
You look into the below program, we done following activities.
- Created a function pointer with typedef "typedef int (*func_op)(int, int);"
- Created a structure, structure members are "function pointer".
- Create a display() function with argument structure type "struct_func"
- In the main() function
- created a struct variable "Operation" and assigned each struct member
- struct members are function pointer so we assigned with function address (function name is base address of function Operation.ptr1 =addition same as Operation.ptr1 =&addition)
- called display() function with argument "Operation" because "Operation" already Initialized properly.
- from the structure member access called function pointers like addition, subtraction and multiplication. e.g., Operation.ptr1(20,5)
- function display() received struct in the variable "op".
- from "op" we called function pointers. e.g., op.ptr1(30,5)
Struct member is function pointer array
This is same as "struct member is function pointer" but it covers one more topic like "array".
Below program is same as previous program, please find the following changes.
- typedef int (*func_basic[3])(int, int); this is array of function pointer.
- from the name space , created structure member "ptrOperation1" by using "func_basic"
- func_basic ptrOperation1; nothing but "ptrOperation1[3]". because of "typedef" name space is "array of function pointer" type.
- In the main() function
- created structure member and Initialized "maths_struct Op_M" with already defined functions "addition, subtraction, multiplication, Integration, differentiation.
- from the struct var accessed and called function "Op_M.ptrOperation1[0](30, 20)"
- Passed structure variable to the function Display(Op_M);
- called array of functions from structure.
This program covers following topics "array of function pointer", "struct member is function array" and How to pass "array of function pointer from structure"? How to access members structure in the called function?
In the previous programs explained how to pass function argument is "struct member is function pointer"? and also How to pass function argument is "struct member is and array of function pointer"?
void Display(maths_struct ); function argument is structure
maths_struct Display(void); function return type is structure
Figure -1: Function return value is "structure". structure has function pointer.
Figure-2: Function return value is "structure". structure has function pointer array
self-referential structure in C
A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers pointing to the same type of structure as their member.
syntax:
struct struct_name{
data_type struct_member;
struct_name *<struct_member>;
}struct_tag;
Below program explanation as follows:
- created structure. name of the structure is "student" with two members, one is int and another one is pointer type of structure again the name of structure is "student"
- In this program structure name and structure member name both are same hence it is self-referenced.
Below two programs clarifies the below "declaration and initialization"
Structure Memory Allocation
If we create an object of some structure, then the compiler allocates contiguous memory for the data members of the structure. size of the structure is "sum of size each structure members and structure padding bytes".
Memory will be allocated to structure when we declare a structure variable.
- When a variable is declared compiler automatically allocates memory for it. This is known as compile time memory allocation or static memory allocation.
- Memory can be allocated for data variables after the program begins execution. This mechanism is known as runtime memory allocation or dynamic memory allocation.
Structure padding
To maintain the data alignment, compute processor assigning an empty memory byte to the structure size this is called "structure padding"
Below diagram and program explains the concept of structure padding.
Program:
In the above diagram explained the memory consume of each memory.
Example-1: char is one byte so next byte is not occupied by "int". due to data alignment, 3 empty memory bytes are added these are called padding bytes. in this example we have two char variables between "int" so 6 padding bytes are added to the structure hence structure size is "16".
Example-2: two char bytes are declared consecutively hence 1st byte and 2nd byte occupied by char, Int start with 4th byte and float started at 8th byte hence structure size is "12".
- In order to align the data in memory, one or more empty bytes (addresses) are inserted (or left empty) between memory addresses which are allocated for other structure members while memory allocation. This concept is called structure padding.
- If the memory is arranged as single bank of one byte width, the processor needs to issue 4 memory read cycles to fetch an integer. It is more economical to read all 4 bytes of integer in one memory cycle. To make use of this advantage of processor, data are always aligned as 4 bytes package which leads to insert empty addresses between other member’s address.
- Because of this structure padding concept in C, size of the structure is always not same as what we think. (please refer student_set1 is 16 byte and student_set2 is only 12 bytes for the same members).
Use of structure and union in embedded field
Structure and union is looks similar because definition syntax and Member access is looks similar.structure defined with keyword "struct" and union defined with "union" keyword.
struct <structure_name> || union <union name>
{ || {
|| datatype union_member1;
datatype struct_member1; || datatype union_member2;
datatype struct_member2; || datatype union_member3;
datatype struct_member3; || } <union tag>;
}<structure tag>;
Major difference is
- Members of a struct each have their own address in memory
- Size of a struct is at least as big as the sum of the sizes of the members
- Members of a union SHARE the same memory
- The size of the union is the size of the largest member
Structures are used
- Define the register fields and access them (Register definition file). Most of the embedded system application like low-level drivers or device drivers using struct
- peripheral register access using the struct facility
- struct bit-field concepts is used in the device status communication and more
Unions are used:
- whenever shared memory access required. unions were creating a shared memory area for mutually exclusive variables.
- Union in C Language for Packing and Unpacking Data
========================================================================================================
Topics like real-time embedded applications of structure and unions are will be discuss in upcoming articles
========================================================================================================
Thanks for visiting the Post..!!
========================================================================================================
No comments:
Post a Comment