Home > Blockchain >  Using function pointer, but got warning makes pointer from integer without a cast [-Wint-conversion]
Using function pointer, but got warning makes pointer from integer without a cast [-Wint-conversion]

Time:07-29

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)){

CodePudding user response:

When you do this:

getVersion(Library_Version(&pVersion))

You're not passing a function pointer to getVersion. You're calling Library_Version and passing the return value of that function to getVersion

This is also invalid:

    xyz_Version *ppVersion;
    int message = (*f)(ppVersion);

Because you're passing an uninitialized pointer to a function.

You want to change getVersion to take both a function pointer and the argument to that function and use them together:

int getVersion( int(*f)(xyz_Version *), xyz_Version *pVersion){
        int message = f(pVersion);
        return message;
}

And call it like this:

res = getVersion(Library_Version, &pVersion);

CodePudding user response:

There are a few problems in the original code:

  1. On line: res = getVersion(Library_Version(&pVersion)); you are calling function Library_Version, but you actually want to pass in a pointer to call it later.
  2. In getVersion you actually attempt to pass in ppVersion, which is an uninitialized pointer. Instead, you should pass in a pointer provided from the caller.

The following should fix the problems that you are having.

    typedef struct XYZ_VERSION{
        int major;
        int minor;
        int build;
    }xyz_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 * pVersion){
    
        int message = (*f)(pVersion); // <-- Call function and used passed in pointer to return version.
        return message;
    }
    
    int main(int argc, char *argv[]){
        int res;
        xyz_Version pVersion;
        res = getVersion(Library_Version, &pVersion); // <-- Pass in pointer to function and pointer to object used to return version info.
        printf("res:%d\n", res);
        return 0;
    }
  •  Tags:  
  • c
  • Related