Home > Mobile >  Don't understand compiler warning - "passing unsigned into to type unsigned int*"
Don't understand compiler warning - "passing unsigned into to type unsigned int*"

Time:10-01

I have a struct type that I have created

typedef struct component {
    char IPaddress[25];
    int serverPortNumber;
    unsigned int handle;
};

I created three instances of that struct. I am using an in-built CVI function for connecting to a TCP server. It has the following form and arguments that I'm providing.

ConnectToTCPServerEx(
    pComponent->handle, pComponent->serverPortNumber, 
    pComponent->IPaddress, ClientTCPCB, NULL, TCP_TIMEOUT, TCP_ANY_LOCAL_PORT)

In code lines that precede this function call, I use the following expression to get the address of a declared component struct that I've already created.

struct component *pComponent = &SomeComponentStruct;

My compiler returns the following

warning: incompatible integer to pointer conversion passing 'unsigned int' to parameter of type 'unsigned int *'; take the address with &

Why am I getting this warning? The CVI function expects the 'handle' argument to be an unsigned int passed by reference (so that it can be written to.) The member of the structure .handle is indeed an unsigned int. But in this case I am passing a pointer to a struct and then using the -> operator to access 'handle'. Adding "&" to that wouldn't make sense, because then I'd be passing a pointer to a pointer.

Maybe it's because the following line

struct component *pComponent = &SomeComponentStruct;

Is in my code actually two lines, I declare a pointer to type struct component, but then use a function to assign it the address of an actual decalred struct component onject (depending on the int parameter).

struct component *pComponent = NULL;

pComponent = setComponent(component) //this function returns the address of a declared struct component

Is the compiler just not catching this?

CodePudding user response:

pComponent->handle is the member object itself, not a pointer to it. pComponent was a pointer, yes, but -> dereferenced it. You do indeed need to do &pComponent->handle, and then you will have a pointer to the object (not a pointer to a pointer). It's perfectly correct.

  •  Tags:  
  • c cvi
  • Related