简体   繁体   English

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

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

I'm working on a function that works as part of a larger program.我正在开发一个 function,它是一个更大程序的一部分。 My C pointer skills are a bit rusty and so I need some help here.我的 C 指针技能有点生疏,所以我需要一些帮助。

Notice: I've omitted a lot of the program because it is irrelevant and works correctly, all you need to know is that I import a header file that declares the struct ImportedStruct , also, I cannot change the function header, so I need to pass the ImportedStruct as a pointer and the function as a pointer.注意:我省略了很多程序,因为它无关紧要并且可以正常工作,您需要知道的是我导入了一个 header 文件,该文件声明了 struct ImportedStruct ,而且,我无法更改 function header,所以我需要将 ImportedStruct 作为指针传递,将 function 作为指针传递。

I get the following error message in VS code: pointer to incomplete class type is not allowed .我在 VS 代码中收到以下错误消息: pointer to incomplete class type is not allowed

Any ideas?有任何想法吗?

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

    // Remaining function ...
}

Header file: Header 档案:

struct ImportedStruct 
{ 
  int width, height;
};

I just compiled the code you provide我刚刚编译了你提供的代码

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);
}

and it compiled successfully.并且编译成功。

Make sure struct definition is included.确保包含结构定义。 Also, you can put另外,你可以把

struct ImportedStruct 
{ 
  int width, height;
};

right before your function definition.就在您的 function 定义之前。 If compiler does not give you multiple definition error, then struct definition is not included by headers.如果编译器没有给你multiple definition错误,那么头文件中不包含结构定义。

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

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