Search code examples
c++void-pointers

return value from reference of address in C++


Sorry if my wording is incorrect but here is the code:

typedef struct
{
    unsigned char bSaveRestore;
    unsigned short wPresetId;
}
uvcx_ucam_preset_t;
...
uvcx_ucam_preset_t data;
data.wPresetId = 0;
data.bSaveRestore = 0;  
setProperty(&data);
...
setProperty(void *data ...)
{
  //How to access wPresetId and bSaveRestore here...
  // The next line is where I'm using it.
  hr = ksControl->KsProperty((PKSPROPERTY)&extProp, sizeof(extProp), data, dataLen, &bytesReturned);}
  /* Here is from hsproxy.h
   STDMETHOD(KsProperty)(
        THIS_
        _In_reads_bytes_(PropertyLength) PKSPROPERTY Property,
        _In_ ULONG PropertyLength,
        _Inout_updates_bytes_(DataLength) LPVOID PropertyData,
        _In_ ULONG DataLength,
        _Inout_opt_ ULONG* BytesReturned
  */

Ultimately, I'm trying to figure out why my camera is rebooting on the line mentioned and other structures seem ok. I just can't see anything from data, but if I break before the call, I can see data is ok. I've tried created a variable void *test = &data just before the call and I have the same problem trying to access data.

I'm unable to figure out how to deal with data inside setProperty. From my playing around in debug:

data is the address.
&data is the address of the address.
*data gives an error "expression must be a point to a complete object type"

Solution

  • data is an object of type uvcx_ucam_preset_t, &data returns the address of data, i.e. a pointer with type uvcx_ucam_preset_t*. setProperty is taking void* as parameter, when passing &data the pointer converts to void* implicitly.

    In setProperty, if you want to access data member wPresetId or bSaveRestore you need to convert the pointer back explicitly. e.g.

    setProperty(void *data)
    {
        uvcx_ucam_preset_t* p = static_cast<uvcx_ucam_preset_t*>(data);
        p->bSaveRestore = ...;
        p->wPresetId = ...;
    }