Part-4
- Pass the function pointer to functions and access functions by "for loop"
- Passing individual members as arguments to function, Function argument is
- Normal Member (like int, char, float double)
- An Array of basic datatype
- Pointer Member
- Function pointer
- Function pointer array
- Structure member is void pointer
========================================================================================================
Pass the function pointer to functions and access functions by using "for loop"
In the below program, we will se how we can pass structure member has "function pointer" and if we declare array of structure then how we can access function pointer assigned functions by using "loop".
Look into the below program
- "func_gen" is the function pointer of two integer arguments and returns int.
- Structure "Operation" is defined with structure member has function pointer "func_gen".
- defined and initialized array of structure "Operation op[4] ={add, sub, mul, division};" this expression telling that Index[0] is add() function, Index[1] is sub() function so on.
- In the main() function
- called "display(op);" ==> It is shows that How to pass array of structure to the function? here we are passing base address of an array. By using pointer increment accessing the functions assigned to "structure member is function pointer"
- we called "calculation()" function, Inside the calculation() we are calling call_functPointer() by using "for loop"
Note: function call from main() and function call from calculation() both are same but this program explains, How global structure can be pass in different function and How we can select Individual function by "for loop" or from switch case.
Below program not only concentrate on the "array of function pointer is pass to function argument" but also it is explains
- How "For loop" is used to access "function pointer array" ?
- How to access functions with different number of arguments?
In the previous program we used only one for loop to access all assigned functions to array of structure but in this program arguments number is different. One function has two arguments and another one is only one argument so we can not access all the functions with one for loop.
Note that, while creating struct members keep in mind that we will access same number of arguments functions by using "for loop." hence we will see the function definition and then define the structure members.
Program explanation as follows:
- func_gen1 and func_gen2 are function pointer array with two argument.
- func_gen3 and func_gen4 are function pointer array with one argument.
- "Operation" structure is created with members. these members are function pointer array. while defining also we placed func_gen1, func_gen2, func_gen3, func_gen4.
- very key point here is placement of struct members we are not created randomly. 1st placed two argument functions then one argument functions. Not mixing of different number arguments function.
- In the main() function "res=calculation(Op_List);" is called.
- Op_List is structure variable. Initialized with all functions.
- In the calculation() function, accessing the functions already assigned.
- Key note-1, first "for loop" started with "Obj1" and accessed "add, sub, mul, division" but here "mul, division" functions are part of "Obj2" but we never used "Obj2" for access because function arguments are same hence array increment always pointing the next function.
- Key note-1 is applicable for another two functions.
Passing individual members as arguments to function, Function argument is Normal member
- Each member is passed as an argument in the function call.
- They are collected independently in ordinary variables in function prototype or function header.
- This is same "call by value" or "pass by value "
In the below program as follows:
- Structure is defined as normal struct members like int, float and double
- we are passing each Individual members to the functions "Add_Int, Add_double, calculation1 and calculation2"
- Observe the function prototype, each argument is normal type
- look function definition "int Add_Int(int, int, int);", Here arguments are normal
Passing individual members as arguments to function, Function argument is an array
In this program, structure member is an array hence we can pass the arguments to function by entire array or we can pass individual index.
- void Check_PassorFail_1(char name[30], float [NUM_SUB]); this function second argument is "array" type hence we pass entire array.
- we call the function "Check_PassorFail_1(St1[i].name, St1[i].marks);". here marks is "array type".
- void Check_PassorFail_2(char name[30], float); this function second argument is "float" type hence we pass array Index value of an array.
- we call the function "Check_PassorFail_2(St1[i].name, St1[i].marks[j]);" here "i" is structure index and "j" is Index of marks variable.
Passing individual members as arguments to function, Function argument is Pointer
Compare to previous program, this program has structure members as pointer variables.
This is same as "Call by reference" or "Pass by reference".
Major changes are follows:
- Now structure members are pointer type
- function definition changed from Normal arguments to pointer arguments
- i.e.. int Add_Int(int*, int*); now we made pointer integer arguments
- structure declaration as address type "str s1={&var1, &var2, &var3};"
- we are passing address of variable to the calling function.
- function sending s1.a nothing but address of "var1"
Passing individual members as arguments to function, Function argument is Function pointer
Here function argument is "function pointer". from the calling function we need to send address of structure variable and it indirectly points to the function.
In this program we are passing entire "function pointer array" or "pass individual functions of function pointer array".
See the function definition:
typedef int (*func_gen[3])(int, int); this is function pointer array
typedef int (*func_normal)(int, int); this is function pointer. it is defined to call individual functions.
Method-1: void calculation_Array(int, func_gen); "func_gen" is an argument of function. this type is "function pointer array".
Method-2: void calculation_Norm(int, func_normal); "func_normal" is the function argument. this type is "function pointer". this method used for access individual functions.
Structure member is Void Pointer
Void pointer is a pointer that has no associated data type with it. void pointer can hold address of any type and any typecasted to any type.
In the below program, we defined "void pointer" as the structure member and from the void pointer we accessed different data.
Note that How to dereference the pointer by typecast.
as per basic understanding, we should type cast to required data type (primary, derived or user-defined).
((int*)S1[0].ptr) ==> It is not dereference the value because it is points to the address of the variable. pointer where it is pointing.
(*(int*)S1[0].ptr) ==> It is dereference the value properly. apply basic principle of Pointer.
asterisk (*) is the value of the variable where pointer pointing.
No comments:
Post a Comment