1. Function pointer
2. Function pointer for C++ class member function
3. Function returning function pointer
4. Function pointer passed as argument
5. Array of function pointers
Function pointer
Its a pointer holding the address of a function.
Declaration
Example function declaration - int myFunction(int, float);
Example function pointer - int (*myFuncPtr)(int, float);
Later in the program code assign the address of myFunction to myFuncPtr.
i.e., myFuncPtr = &myFunction;
Sample program
#include "stdafx.h"
//Declare a function
int myFuncAdd(int,int);
int myFuncMultiply(int,int);
//Declare function pointer1
int (*myAddPtr)(int,int);
int main(int argc, char* argv[])
{
myAddPtr = &myFuncAdd;
int result = myAddPtr(10,20);
printf("Added value %d\n",result);
//Using same function pointer for different function.
//You can do this as long as, function signatures are same.
myAddPtr = &myFuncMultiply;
result = myAddPtr(10,20);
printf("Multiplied value using add pointer %d\n",result);
//Declaration of function pointer2
int (*myMulPtr)(int,int) = &myFuncMultiply;
result = myMulPtr(10,20);
printf("Multiplied value %d\n",result);
return 0;
}
int myFuncAdd(int data1,int data2)
{
return data1 + data2;
}
int myFuncMultiply(int data1,int data2)
{
return data1 * data2;
}
-------------------------------------------------------------------------------------------------
Function pointer for C++ class member function
All the non static C++ class member function pointer declaration contains a hidden parameter, which is the "this" pointer of instance of the class. So the function pointer for static member and non-static member are incompatible.
Example declaration
int (MyClass::*myFuncAddPtr)(int,int) = NULL;
Here myFuncAddPtr is a pointer to a function in a class called MyClass, which takes two integers as arguments and returns an integer.
Sample program
#include "stdafx.h"
class myClass
{
public:
int myFuncAdd(int data1,int data2)
{
return data1 + data2;
}
};
//Declare function pointer for myClass::myFuncAdd
int (myClass::*myFuncAddptr)(int,int) = NULL;
int main(int argc, char* argv[])
{
myClass myClassobj;
myFuncAddptr = &myClass::myFuncAdd;
result = (myClassobj.*myFuncAddptr)(20,30);
printf("Added value using C++ function pointers %d\n",result);
return 0;
}
-------------------------------------------------------------------------------------------------
Passing function pointer:
Function pointer can be passed as a argument for a function. Below is the example for passing function pointer.
Returning function pointer:
Function can return pointer to a function. This is bit tricky and confusing....see below for the declaration of the function call which return function pointer.
//Passing function pointer
int PassFuncPtr(int (*myFuncPtr)(int ,int ))
{
return myFuncPtr(10,60);
}
typedef int (*myFuncptr)(int,int);
myFuncptr retPointer()
{
int(*myFuncPtr)(int,int) = &myFuncAdd;
return myFuncPtr;
}
//Function definition with out using type def
int (*retPointer2())(int,int)
{
return &myFuncAdd;
}
inside main()
//Passing function pointer
int (*myFuncPtr)(int,int) = &myFuncAdd;
result = PassFuncPtr(myFuncPtr);
printf("Added value using passing function pointers %d\n",result);
//Returning function pointer
int (*myFuncPtr2)(int,int) = NULL;
myFuncPtr2 = retPointer();
result = myFuncPtr2(20,10);
printf("Added value using returned function pointer %d\n",result);
-------------------------------------------------------------------------------------------------
Array of function pointers: This is also bit tricky and confusing. I feel, using typedef is the easy way to implement array of function pointers.
below code is inside the main()
//Array of function pointers
typedef int (*myArrayFuncPtr)(int,int);
myArrayFuncPtr funcPtrArray[10] = {NULL};
//Without using typedef
int (*myArrayFuncPtr2[10])(int,int) = {NULL};
funcPtrArray[0] = &myFuncAdd;
result = (*funcPtrArray[0])(10,10);
printf("Added value using Array of function pointer %d\n",result);
-------------------------------------------------------------------------------------------------
This is just to share my understanding. If something is wrong please revert me with questions and details
Tuesday, January 5, 2010
C Storage classes
Storage classes specifies where the variable need to be created, life time of the variable and scope of the variable.
There are four storage classes supported in C programming language.
1. Auto.
2. Register.
3. Static.
4. Extern.
When discussing about the storage classes, we mainly need to consider the life of the variable, scope of the variable(means is it possible to access this variable this at this point of time?) and storage location of the variable.
1. Auto specifier is the default storage class specifier for variables declared inside a function.
This variable life remains will be with in the scope of the function. i.e, when function scope } ended, this variable will go out of the memory and will not be accessible.
2. Register specifier also similar to auto variable but the storage location is CPU Registers instead of RAM. So the variables can be accessed by the processor quickly compared to the variables stored in RAM. Also it is not guaranteed that register keyword, will store the variable in CPU registers.
"What is the CPU register name which stores the variable when register keyword is used to declare the variable?"
3. Static storage specifier, will create the global variable(i.e life time is till end of the program) which can be accessed only with in the scope of the declaration.
So, when static variable is declared inside a function, then it can be accessed only with in the function, and its value initialized during the first call of the function. Value retained till end of the program. Example usage: 1. Static counters. 2. Avoid localization issues(Hide the variable to outside the scope)
4. Extern storage specifier, indicates that variable might be defined in some other source file and reference of that global variable is declared and used in some other source file.
What is the difference between static and global variables?
Answer1
Variables defined local to a function disappear at the end of the function scope. So when we call the function again, storage for variables is created and
values are reinitialized. So if we want the value to be extent throughout the life of a program, we can define the local variable as "static." Initialization is performed only at the first call and data is retained between func calls.
Had it been gloal variable, it would have been available outside the scope of the function, but static variable is not available outside the scope of a function (helpful in localizing errors - as it can't be changed outside the func
scope).
Answer2
Static and global variable differ a lot in their behaviour to life and scope. First, let me distinguish between life and scope. Life of an object determines whether the object is still in the memory (of the process) whereas scope of the object is whether can I know the variable by its name at this position. It is possible that object is live, but not visible (not in scope) but not that object is not alive but in scope (except for dynamically allocated objects where you refer object through pointers).
Static variables are local in scope to their module in which they are defined, but life is throughout the program. Say for a static variable inside a function cannot be called from outside the function (because it's not in scope) but is alive and exists in memory. The next time this function is entered (within the same program) the same chunk of memory would be accessed now retaining the variables old value and no new memory is allocated this time for this variable like other variables in the function (automatic variables). So basically the variable persists throughout the program. Similarly if a static variable is defined in a global space (say at beginning of file) then this variable will be
accessible only in this file (file scope).
There are four storage classes supported in C programming language.
1. Auto.
2. Register.
3. Static.
4. Extern.
When discussing about the storage classes, we mainly need to consider the life of the variable, scope of the variable(means is it possible to access this variable this at this point of time?) and storage location of the variable.
1. Auto specifier is the default storage class specifier for variables declared inside a function.
This variable life remains will be with in the scope of the function. i.e, when function scope } ended, this variable will go out of the memory and will not be accessible.
2. Register specifier also similar to auto variable but the storage location is CPU Registers instead of RAM. So the variables can be accessed by the processor quickly compared to the variables stored in RAM. Also it is not guaranteed that register keyword, will store the variable in CPU registers.
"What is the CPU register name which stores the variable when register keyword is used to declare the variable?"
3. Static storage specifier, will create the global variable(i.e life time is till end of the program) which can be accessed only with in the scope of the declaration.
So, when static variable is declared inside a function, then it can be accessed only with in the function, and its value initialized during the first call of the function. Value retained till end of the program. Example usage: 1. Static counters. 2. Avoid localization issues(Hide the variable to outside the scope)
4. Extern storage specifier, indicates that variable might be defined in some other source file and reference of that global variable is declared and used in some other source file.
What is the difference between static and global variables?
Answer1
Variables defined local to a function disappear at the end of the function scope. So when we call the function again, storage for variables is created and
values are reinitialized. So if we want the value to be extent throughout the life of a program, we can define the local variable as "static." Initialization is performed only at the first call and data is retained between func calls.
Had it been gloal variable, it would have been available outside the scope of the function, but static variable is not available outside the scope of a function (helpful in localizing errors - as it can't be changed outside the func
scope).
Answer2
Static and global variable differ a lot in their behaviour to life and scope. First, let me distinguish between life and scope. Life of an object determines whether the object is still in the memory (of the process) whereas scope of the object is whether can I know the variable by its name at this position. It is possible that object is live, but not visible (not in scope) but not that object is not alive but in scope (except for dynamically allocated objects where you refer object through pointers).
Static variables are local in scope to their module in which they are defined, but life is throughout the program. Say for a static variable inside a function cannot be called from outside the function (because it's not in scope) but is alive and exists in memory. The next time this function is entered (within the same program) the same chunk of memory would be accessed now retaining the variables old value and no new memory is allocated this time for this variable like other variables in the function (automatic variables). So basically the variable persists throughout the program. Similarly if a static variable is defined in a global space (say at beginning of file) then this variable will be
accessible only in this file (file scope).
Subscribe to:
Posts (Atom)