Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ struct initialization assertion fails

Tags:

c++

assert

struct

#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 in place of ??? to have a successful assertion?
I used assert(structu.z == 0); but unfortunately got the error
int main(): Assertion 'structu.z == 0 failed.Aborted'

like image 721
NeoPhoenix Avatar asked Mar 19 '26 05:03

NeoPhoenix


1 Answers

You want:

 assert(structu.z == 0);

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