c++ initialize struct code example
Example 1: c++ 20 struct initialization
#include <iostream>
#include <filesystem>
struct hello_world {
const char* hello;
const char* world;
};
int main ()
{
hello_world hw = {
.hello = "hello, ",
.world = "world!"
};
std::cout << hw.hello << hw.world << std::endl;
return 0;
}
Example 2: how to initialize an struct object in c++
struct Coordonnees
{
int x;
int y;
}
int main()
{
Coordonnees coords = {1,2};
}
Example 3: c++ initialize a struct
struct address {
int street_no;
char *street_name;
char *city;
char *prov;
char *postal_code;
};
address temp_address = {
0,
nullptr,
"Hamilton",
"Ontario",
nullptr,
};