繁体   English   中英

通过 function 传递结构指针然后访问结构数据

[英]Passing struct pointer through a function then accessing struct data

我正在开发一个 function,它是一个更大程序的一部分。 我的 C 指针技能有点生疏,所以我需要一些帮助。

注意:我省略了很多程序,因为它无关紧要并且可以正常工作,您需要知道的是我导入了一个 header 文件,该文件声明了 struct ImportedStruct ,而且,我无法更改 function header,所以我需要将 ImportedStruct 作为指针传递,将 function 作为指针传递。

我在 VS 代码中收到以下错误消息: pointer to incomplete class type is not allowed

有任何想法吗?

#include "name.h"
long int *functionName(struct ImportedStruct *structName, int *irrelevant){
    int width = structName->width;
    int height = structName->height;

    // Remaining function ...
}

Header 档案:

struct ImportedStruct 
{ 
  int width, height;
};

我刚刚编译了你提供的代码

struct ImportedStruct 
{ 
  int width, height;
};

long int *functionName(struct ImportedStruct *structName, int *irrelevant){
    int width = structName->width;
    int height = structName->height;

    // Remaining function ...
    return (long int *)(width + height);
}

并且编译成功。

确保包含结构定义。 另外,你可以把

struct ImportedStruct 
{ 
  int width, height;
};

就在您的 function 定义之前。 如果编译器没有给你multiple definition错误,那么头文件中不包含结构定义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM