dpc
dpc

Reputation: 31

Clearing a C array to zero when passed as a member of a pointer to a struct

In C++ when I have a struct definitions

struct foo
{
    ...
};

struct bar
{
    foo FooData[10];
    ...
};

And I want to write a function

void Baz(bar *Bar)
{
    Bar->FooData = {};
}

I get an error (C3079 in Visual Studio Community 2013) "an initializer-list cannot be used as the right operand of this assignment operator."

Yet this syntax seems to work fine in a situation like

int Array[10];
Array = {};
...

Why does the previous example fail to compile?

Upvotes: 0

Views: 210

Answers (1)

paulsm4
paulsm4

Reputation: 121629

Regarding your question: you can use the "{}" when you define your array ... but not afterwards.

Regarding the problem, I'd recommend memset

memset(Bar->FooData, 0, sizeof (bar->FooData));

If you wanted to allocate FooData dynamically, you could define your struct like this:

struct bar
{
    foo *FooData;
    ...

Upvotes: 1

Related Questions