So if I was given a the following 2 functions:
void printhex(int x, char y, char z)
{
printf("%x%x%x,x,y,z);
}
void printdecimal( int x, char y ,char z)
{
printf("%i%i%i,x,y,z);
}
How would i define a variable, that is a function pointer, that can be assigned either of the 2 given functions?
Also within the main function how would i determine if any command line arguments were entered after the program name when the program was executed. If anything was entered, how would I be able to assign the function pointer to the function printhex, but otherwise would assign the function to printdecimal?
You could declare a variable similarly to:
void (*printSomething)(int, char, char);
You could assign it to one of those functions with:
printSomething = &printhex;
or
printSomething = &printdecimal;
The & is completely optional, so you could also just write printSomething = printhex;
for example.
You also didn't ask, but you can call the function through the pointer with (for example):
(*printSomething)(1, 2, 3);
The * is also optional, so you could just write:
printSomething(1, 2, 3);
If you need help with command line arguments as well, that should be a separate question.
A1) This is how the function pointer will look like:
void (*myFuncPtr)(int, char, char);
You then assign a function to it like this:
myFuncPtr = &printhex;
And call it like this:
(*myFuncPtr )(1, 'a', 'b');
A2) The argc
variable of the main()
will tell if any command line arguments were entered other than the program name itself. If argc > 1
, then argc - 1
other strings were passed when calling the program. This is because argc
also counts the program name as one of the parameters. So something like this should work for you:
if (1 < argc)
{
myFuncPtr = &printhex;
}
else
{
myFuncPtr = &printdecimal;
}
Use a typedef
void printhex(int x, char y, char z) {
printf("%x%x%x",x,y,z);
}
void printdecimal( int x, char y ,char z) {
printf("%i%i%i",x,y,z);
}
typedef void (*printer)(int x, char y ,char z);
printer hex = &printhex;
printer dec = &printdecimal;
If I understand you correctly, it should be simple:
void (* ptr)(int, char, char);
ptr = printhex;
(*ptr)(a, b, c); //this will call the function
Such variable would be declared as
void (*fptr)(int, char, char);
then assigned a value:
fptr = printhex;
and used to invoke a function:
fptr(13, 'a', 'b');
Or you may declare a new name for the function type:
typedef void (*PrintFuncPtr)(int, char, char);
then use it for a variable declaration:
PrintFuncPtr fptr;
To define a function pointer you would use the following syntax:
return-type (*pointer name)(parameters);
in this case, you would use: void (*fp)(int, char, char);
for a function pointer named fp
suitable for the two functions you've defined.
Assigning a function pointer can be done like any other pointer, except you use the name of the function. For example, fp = printhex;
would do exactly what it says: assigning the address of printhex
to the pointer fp
. You don't need to use the &
operator for printhex
because the identifier is implicitly converted to pointer-to-function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.