I got a problem that I want to use function pointer concept, but I in loop make me confuse.
I will try to describe my question clearly.
I want to create a template as read version function, that I can use getVersion API with differnt version function to get I want.
e.g. Library_Version, Traffic_Version and so on.
Like this, getVersion(Library_Version), getVersion(Traffic_Version)..
I'm not sure that this method keyword is called function pointer.
If you know the currect keyword, please tell me.
Thanks for your reading and help.
#include <stdio.h>
typedef struct XYZ_VERSION{
int major;
int minor;
int build;
}xyx_Version;
int Library_Version(xyz_Version *pVersion){
xyz_Version version = {0};
version.major = 4;
version.minor = 0;
version.build = 3;
*pVersion = version;
return 666;
}
int getVersion( int(*f)(xyz_Version *pVersion)){
xyz_Version *ppVersion;
int message = (*f)(ppVersion);
return message;
}
int main(int argc, char *argv[]){
int res;
res_Version pVersion;
res = getVersion(Library_Version(&pVersion));
printf("res:%d\n", res);
return 0;
}
BUG:
linn@linn:~$ gcc -c funcPointer.c
funcPointer.c: In function ‘main’:
funcPointer.c:36:19: warning: passing argument 1 of ‘getVersion’ makes pointer from integer without a cast [-Wint-conversion]
36 | res = getVersion(Library_Version(&pVersion));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| int
funcPointer.c:27:22: note: expected ‘int (*)(xyz_Version *)’ {aka ‘int (*)(struct XYZ_VERSION *)’} but argument is of type ‘int’
27 | int getVersion( int(*f)(xyz_Version *pVersion)){