c++ - 从C++中的地址引用返回值

标签 c++ void-pointers

抱歉,如果我的措词不正确,但是代码如下:

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
  */
最终,我试图弄清楚为什么我的相机在上述线路上重新启动,而其他结构似乎还可以。我只是看不到数据中的任何内容,但是如果在通话之前中断,我可以看到数据还可以。我试过在调用之前创建一个变量void * test =&data,尝试访问数据时遇到相同的问题。
我无法弄清楚如何处理setProperty中的数据。从我在调试中玩:
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"

最佳答案

datauvcx_ucam_preset_t类型的对象,&data返回data的地址,即uvcx_ucam_preset_t*类型的指针。 setPropertyvoid*为参数,当传递&data时,指针将隐式转换为void*
setProperty中,如果要访问数据成员wPresetId bSaveRestore,则需要显式转换指针。例如

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

关于c++ - 从C++中的地址引用返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63026018/

相关文章:

c++ - void* 转换得到意外的输出

C++ sizeof 相同值的不同结果

c++ - 为什么我必须重新解释_cast 指针指针?

c - 无效指针 : what is wrong?

C++ 错误代码 C1004

C:如何访问存储在空指针(void *)中的函数指针?

c - 如何将结构中的空指针分配给另一个结构?

c++ - 如何遍历具有结构指针的 vector ?

c++ - 特定的 C++ 宏

c++ - std::vector 的表达式模板运算符重载问题