initialize struct C 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++

// exemple avec la structure Coordonnees :
struct Coordonnees
{
  int x;
  int y;
}

int main()
{
  Coordonnees coords = {1,2};
}

Example 3: how to initialize a struct in c

typedef struct MY_TYPE {
  bool flag;
  short int value;
  double stuff;
} MY_TYPE;

void function(void) {
  MY_TYPE a;
  ...
  a = { true, 15, 0.123 }
}

Example 4: objective c struct initialization

Struct initialization

Tags:

Cpp Example