Pointers

04 May 2010

The mbed compiler choked on this bit of code:

/* USB Endpoint Events Callback Pointers */

void (* const USB_P_EP[16]) (uint32_t event) = {

P_EP(0),

P_EP(1),

P_EP(2),

P_EP(3),

P_EP(4),

P_EP(5),

P_EP(6),

P_EP(7),

P_EP(8),

P_EP(9),

P_EP(10),

P_EP(11),

P_EP(12),

P_EP(13),

P_EP(14),

P_EP(15),

};

Can anybody explain on the correct format for this?

04 May 2010

"Choked" is a bit vague. Show us the definition of P_EP and the full error message.

04 May 2010

Sorry about 'choked'

#define P_EP(n) ((USB_EP_EVENT & (1 << (n))) ? USB_EndPoint##n : NULL)

The error message is:

"A value of type "void *" cannot be used to initialize an entity of type "void (*const)(std::uint32_t)" (E144)"

04 May 2010

I can't reproduce it with your sample right now, but I had this problem once. As far as I remember, it goes like this:

In C, NULL is defined as (void*)0.
In C++, NULL is defined as 0.
In C++, void* cannot be cast to a function pointer.

So, check your include files, it's likely that one of them is defining NULL as (void*)0 without checking for C++ mode. The correct definition should look like this:

#ifndef NULL
#ifdef __cplusplus
#define NULL    0
#else
#define NULL    ((void *)0)
#endif
#endif

04 May 2010

I did find NULL defined as:

#ifndef NULL

 

#define NULL    ((void *)0)

#endif

It was in the NXP type.h header file. Putting in your definition fixed it.
I have another similar problem and I'm wondering if this too is a C to C++ issue.
It occurs two times on code lines:
(uint8_t *)pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
(uint8_t *)pD += pD->bLength;
with the same error message:
"Expression must be a modifiable lvalue (E137)"

 

04 May 2010 . Edited: 04 May 2010

You again didn't provide definitions, but I can guess that pD is a pointer to another USB structure. You cannot use such expression in C++. Try to rewrite it, for example:

uint8_t * pNextD = (uint8_t *)pD + ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
pD = (TYPE_OF_D*)pNextD;
pNextD = (uint8_t *)pD  + pD->bLength;
pD = (TYPE_OF_D*)pNextD;

Do not afraid to be verbose, modern compilers are pretty good at optimizing sequences of complex expressions, and readable code is better for humans.