Search code examples
cstructuretypecasting-operator

Typecasting int to char *


Following definition of structure is defined in one of my header file

typedef struct REMDEV_ADDR {
UINT8    addr[DEVICE_ADDR_SIZE];
} RemDev_Addr;

typedef RemDev_Addr BTDEV_ADDR;

Now I have following function which I want to use.

 hci_acl_connect(UCHAR *   bd_addr,UINT16 *  handle);

So I made a global instance of the above structure in my c file

BTDEV_ADDR hsu_addr

and calling the function like this

hci_acl_connect((unsigned char *)&hsu_addr,&cont_hand);

Is typecasting is correct "(unsigned char *)&hsu_addr" ?


Solution

  • Yes, of course. Your variable resides on stack, but you need to provide a pointer, so you use &your_var. You need a pointer to UCHAR, so you cast: (UCHAR *)&your_var.

    The casting itself is OK, but we don't know what UCHAR * bd_addr is supposed to represent. Perhaps you should pass your_var.addr instead?