0

Ok let me explain a little more. I have a pointer to structure AB type struct1 inside the structure there is a pointer to another structure called CD of type struct2 and the definition of struct2 have some prototypes for functions including Func(param). If someone can help me understand a little I will be very thankfull.


Yes, the code is for a USB host in a microcontroller I didn't add the code because it could drive the question away, but here it is,

The call is:

void USBHOST_Process(USB_OTG_CORE_HANDLE *pdev , USBH_HOST *phost){
    ...
    if (phost->usr_cb->UserInput() == USBH_USR_RESP_OK){
    ...
    }
    ...
}


typedef struct _Host_TypeDef
{
  HOST_State            gState;       /*  Host State Machine Value */
  HOST_State            gStateBkp;    /* backup of previous State machine value */
  ENUM_State            EnumState;    /* Enumeration state Machine */
  CMD_State             RequestState;       
  USBH_Ctrl_TypeDef     Control;

  USBH_Device_TypeDef   device_prop; 

  USBH_Class_cb_TypeDef               *class_cb;  
  USBH_Usr_cb_TypeDef                 *usr_cb;


} USBH_HOST, *pUSBH_HOST;


typedef struct _USBH_USR_PROP
{
  void (*Init)(void);       /* HostLibInitialized */
  void (*DeInit)(void);       /* HostLibInitialized */  
  void (*DeviceAttached)(void);           /* DeviceAttached */
  void (*ResetDevice)(void);
  void (*DeviceDisconnected)(void); 
  void (*OverCurrentDetected)(void);  
  void (*DeviceSpeedDetected)(uint8_t DeviceSpeed);          /* DeviceSpeed */
  void (*DeviceDescAvailable)(void *);    /* DeviceDescriptor is available */
  void (*DeviceAddressAssigned)(void);  /* Address is assigned to USB Device */
  void (*ConfigurationDescAvailable)(USBH_CfgDesc_TypeDef *,
                                     USBH_InterfaceDesc_TypeDef *,
                                     USBH_EpDesc_TypeDef *); 
  /* Configuration Descriptor available */
  void (*ManufacturerString)(void *);     /* ManufacturerString*/
  void (*ProductString)(void *);          /* ProductString*/
  void (*SerialNumString)(void *);        /* SerialNubString*/
  void (*EnumerationDone)(void);           /* Enumeration finished */
  USBH_USR_Status (*UserInput)(void);
  int  (*UserApplication) (void);
  void (*DeviceNotSupported)(void); /* Device is not supported*/
  void (*UnrecoveredError)(void);

}
USBH_Usr_cb_TypeDef;

Now I understand what it does, but I can't find the definition of UserInput(void) I can't find what it does.

2 Answers2

3

What you call "some prototypes for functions including Func(param)" is a function pointer. You can invoke a function pointed to by the pointer as if the pointer represented that function's name. The inner-most structure has been set up with a pointer to some function; your code snippet invokes that function through its pointer.

Here is a short example that illustrates how this works (also on ideone):

#include <stdio.h>

typedef struct {
    void (*func)(int);  
} with_fp;

void test(int n) {
    printf("Test %d\n", n);
}

int main(void) {
    with_fp s = {.func = test};
    with_fp *ptr = &s;
    ptr->func(123);
    return 0;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

It calls the function Func() of the structure struct2 with those parameters.

The function Func() is a pointer to a function, a pointer (a memory address) where the instructions which make up the function start.

See How do function pointers in C work? for explanations.

The pointer, being a variable, has to be set prior to being used for the call.

Community
  • 1
  • 1
Flavius
  • 13,566
  • 13
  • 80
  • 126
  • 1
    "member function"? This is tagged 'C'. –  Oct 26 '12 at 21:34
  • I know. It's analogous to "member variable", it's not a member function though as you know it from C++, but it makes it easier for him to grasp the concept. – Flavius Oct 26 '12 at 21:35
  • @H2CO3 every other question about C++ is tagged 'c'. – n. m. could be an AI Oct 26 '12 at 21:36
  • @H2CO3 If a C++ question is tagged 'c', edit it and re-tag, don't treat it as if it was about C. In this case it's not clear what the question is about as we don't see the code. – n. m. could be an AI Oct 26 '12 at 21:47
  • @n.m. from the text of the question, it cannot be decided if it's about C or C++ - both languages have the same syntax for two different tasks. So I assumed the question wasn't mistagged, that's why I think the 'member function' terminology is inaccurate. –  Oct 26 '12 at 21:51
  • @H2CO3 Yeah, that's why I've removed it. So why still the debate? – Flavius Oct 26 '12 at 21:56
  • @Flavius One, I didn't notice the edit, sorry. Two, n.m. didn't fully get what I meant, so I wanted to explain my argument more clearly. –  Oct 26 '12 at 21:59