C ++ structure initialization statement fails

#include <cassert>
#include <string>
struct AStruct 
{ 
    int x; 
    char* y; 
    int z; 
};
int main()
{ 
    AStruct structu = {4, "Hello World"};
    assert(structu.z == ???);
}

What should I write instead ???for a successful statement?
I used assert(structu.z == 0);, but unfortunately got an error
int main(): Assertion 'structu.z == 0 failed.Aborted'

+3
source share
3 answers

Do you want to:

 assert(structu.z == 0);

Your code assigns to the z-member instead of testing it. And if you get a message that your edited question says what you did, your compiler is broken. Which of them?

+5
source

โ€œSuccessfully,โ€ I assume that you mean one that does not generate an error message. You probably want:

assert(structu.z == 0);

Please note that I use ==, not =.

, structu.z 0.

+3

assert(structu.z == 0)should work because the element zwill be initialized with a value.

+3
source

All Articles