简体   繁体   English

指向包含数组的结构的指针

[英]Pointer to struct containing array

I have a simple struct containing an array of ints and an index to be used for that array. 我有一个简单的结构,其中包含一个整数数组和一个用于该数组的索引。

#define BUFF_SIZE 100
typedef struct _buffer Buffer;

struct _buffer {
    int buff[BUFF_SIZE];
    int index;
};

I am trying to write a function to create a pointer to an instance of this struct, and initialise its values to zero. 我正在尝试编写一个函数来创建一个指向该结构实例的指针,并将其值初始化为零。 However when I try to initialise the array using the arrow operator I get an error that the expression is invalid. 但是,当我尝试使用箭头运算符初始化数组时,出现错误,表明表达式无效。

...
Buffer* newBuff;
newBuff->buff = {0}; //Error occurs here
...

I know that in c an array is a pointer to the base of the memory block in which the array resides, but was under the impression that dereferencing the struct pointer would allow me to access the array. 我知道在c中,数组是指向数组所驻留的内存块基址的指针,但是给人的印象是,取消引用struct指针将使我可以访问数组。 Am I wrong about this, or am I missing something here? 我对此是否有错,还是我在这里错过了一些东西? I have a feeling that it is something very simple, but for the life of me I just can't spot it. 我感觉这很简单,但是对于我的一生,我只是无法发现。

Thanks! 谢谢!

EDIT: Thanks for the quick answers guys, they've helped me understand some of what is occurring. 编辑:感谢你们的快速解答,他们帮助我了解了一些正在发生的事情。 I neglected to mention that this is intended to be run on an embedded system (sorry about that), so I would like to avoid using includes or malloc if at all possible. 我忽略了提及它打算在嵌入式系统上运行(对此感到抱歉),因此我尽可能避免使用include或malloc。

There are several types of confusion here. 这里有几种类型的混乱。 A pointer points to memory (duh), but you need to get that memory from somewhere. 指针指向内存(duh),但是您需要从某个地方获取该内存。 How you initialize that memory is separate. 初始化内存的方式是独立的。

You can allocate your structure on the stack by declaring a local variable inside your function: 您可以通过在函数内声明一个局部变量来在堆栈上分配结构:

// Initialize all buff elements and index to 0
// Note: Buffer, not Buffer*
Buffer newBuf = { {0}, 0 };  

Or you can allocate it on the heap: 或者,您可以在堆上分配它:

Buffer *newBuf = malloc(sizeof(Buffer));
memset(newBuf, 0, sizeof(Buffer));

When allocating objects on the stack, they are only valid while the current function is executing. 在堆栈上分配对象时,它们仅在当前函数执行时有效。 You can't return a pointer to an object on the stack. 您不能返回指向堆栈上对象的指针。 Further, stack space is typically limited, so you can't put megabyte-sized objects there. 此外,堆栈空间通常有限,因此您不能在其中放置兆字节大小的对象。 When allocating memory on the heap with malloc() , you need to take care to free() when it is not used any more, otherwise you leak memory. 当使用malloc()在堆上分配内存时,如果不再使用free() ,则需要小心,否则会泄漏内存。

You see that when allocating objects on the stack, you are able to use an initializer-list { {0}, 0 } . 您会看到在堆栈上分配对象时,可以使用初始化列表{ {0}, 0 } In the heap case, you can not do that and you have to zero the memory manually using memset. 在堆的情况下,您不能这样做,而必须使用memset手动将内存清零。

The problem is not with the arrow -> operator, it's with the right side of the assignment: {0} is allowed in initializers, but you cannot assign an array like that. 问题不在于箭头->运算符,而在于赋值的右侧:初始化程序中允许使用{0} ,但是您不能像这样分配数组。 You need to use memset to zero out the elements of the array: 您需要使用memset将数组元素归零:

memset(newBuff->buff, 0, sizeof(newBuff->buff));

Note: your code does not set the newBuf to a valid location in memory. 注意:您的代码未将newBuf设置为newBuf中的有效位置。 You need to allocate memory first, like this: 您需要首先分配内存,如下所示:

newBuff->buff = malloc(sizeof(*newBuff));

I have to disagree with @dasblinkenlight, but I think the problem is that newBuff is a pointer, but it hasn't been given a value. 我必须不同意@dasblinkenlight,但我认为问题是newBuff是一个指针,但尚未提供值。 Hence, when you dereference it, you'll probably get a crash or unpredictable results. 因此,当您取消引用它时,您可能会崩溃或无法预测的结果。

You'd have to have a statement that's something like: 您必须拥有类似以下内容的声明:

newBuff = malloc(sizeof (Buffer));

to allocate space of the appropriate size and assign it to newBuff. 分配适当大小的空间并将其分配给newBuff。

Unless I'm missing something terribly obvious... 除非我错过了非常明显的东西...

From your question I was able to understand- you want to initialize all array elements of structure variable buff[ ] to zero. 通过您的问题,我能够理解-您要将结构变量buff[ ]所有数组元素初始化为零。

memset() function from <string.h> file will help you to solve this problem. <string.h>文件中的memset()函数将帮助您解决此问题。

As you said you are writing code for embedded system so there is no need to include whole <string.h> file in your program 如您所说,您正在为嵌入式系统编写代码,因此无需在程序中包含整个<string.h>文件

I tried to replicate your program and found the following solution- 我试图复制您的程序,发现以下解决方案-

void *memset(void * , int , size_t ) __attribute__((__nonnull__(1)));

#define BUFF_SIZE 100

typedef struct _buffer Buffer;

struct _buffer {
    int buff[BUFF_SIZE];
    int index;
};

int main (void)
{
    Buffer* newBuff;
    memset(newBuff->buff, 0, BUFF_SIZE);
}

Please let me know if it was helpful. 请让我知道是否有帮助。

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

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