简体   繁体   English

如何使用指向结构的指针

[英]How to use pointers to structures

I have defined a structure in one of my source code files. 我已经在一个源代码文件中定义了一个结构。 Also I have defined pointer to that structure like (global declaration) 我也定义了指向该结构的指针,例如(全局声明)

struct blockPropStruct { int random_id; int id; CvPoint left_up; CvPoint right_down; };

typedef struct blockPropStruct *newBlock;

In the same file I am assigning memory to those pointers using malloc to create array of structures. 在同一文件中,我使用malloc创建结构数组,将内存分配给这些指针。

newBlock = (blockPropStruct*)malloc(maxNum*sizeof(blockPropStruct));

Now I am trying yo use it in some other source file by declaring (global declaration) 现在,我尝试通过声明(全局声明)在其他一些源文件中使用它

extern struct blockPropStruct *newBlock;

Now when I use something like 现在当我使用类似

newBlock[i].left_up.x=mark1[i];

It throws up an error. 它抛出一个错误。

The name of your structure is struct blockPropStruct . 您的结构名称为struct blockPropStruct Note that in C, you can't just remove the struct part, it's part of the type's name. 请注意,在C语言中,您不能仅删除struct部分,它是类型名称的一部分。 You can define a type for your structure so you have less typing, but I think it's better to remember to use your structures as struct blockPropStruct . 您可以为结构定义类型,以减少类型输入,但是我认为最好记住将结构用作struct blockPropStruct

Based on the error messages you have added in your comments: 根据您在注释中添加的错误消息:

error C2036: 'blockPropStruct *' : unknown size 错误C2036:“ blockPropStruct *”:未知大小
error C2027: use of undefined type 'blockPropStruct' 错误C2027:使用未定义的类型'blockPropStruct'

You are attempting to get the size sizeof(blockPropStruct) in your malloc() call since blockPropStruct is not a valid identifier so you are attempting to get the size of an undefined type. 您正在尝试通过malloc()调用获取sizeof(blockPropStruct)的大小,因为blockPropStruct不是有效的标识符,因此您尝试获取未定义类型的大小。

To define your type: 定义类型:

typedef struct blockPropStruct blockPropStruct;

Now you can refer to your structure type as blockPropStruct or struct blockPropStruct . 现在,您可以将您的结构类型称为blockPropStructstruct blockPropStruct

看来您已经在声明extern变量的文件中省略了struct类型的声明。

您需要使用“ typedef”命名并指向它。

newBlock = (blockPropStruct*)malloc(maxNum*sizeof(blockPropStruct));

To actually for the above statement to work, the current source file should see the size of structure. 为了使上述语句真正起作用,当前的源文件应该看到结构的大小。 So, check whether you have included the corresponding header file. 因此,请检查是否包含相应的头文件。


extern struct blockPropStruct *newBlock;

And when you are doing - 当你做的时候-

newBlock[i].left_up.x=mark1[i];

You should bring the definition of the blockPropStruct to the current compilation unit to be able to use it members. 您应该将blockPropStruct的定义带入当前的编译单元,以便能够使用其成员。 So, try - 所以,尝试-

#include "blockPropStruct.h"
extern struct blockPropStruct *newBlock;  // This says to use else where 
                                          // initialized newBlock
// ....
newBlock[i].left_up.x=mark1[i];           // And to do this, bring the definition
                                          // to this file scope

And there is no need to explicitly typecast malloc . 无需显式类型转换malloc

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

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