I have a requirement in my testing as when I am calling a function I am initializing the structure with 0. But, the moment function ex() gets called, ab the object of struct abc caontains garbage.
But I need to the structure abc should get initialized with 0.
Because I don’t have access to function ex(). So, whatever things I need to set, I need to set from main().
struct abc{
int a[4];
};
void ex()
{
abc ab;
printf("%d\n", ab.a);//Garbage value
}
int main()
{
abc ab;
memset(&ab, 0, sizeof(abc));
printf("%d\n", ab.a);
return 0;
}
Please help.
You can give
abca default constructor that initializes the elements of the array to0. This gets rid of the garbage values:Next, if you want
myexto print the data of theabccreated inmain, you should give it anabcreference parameter. This is a C++11 version ofmyex: