struct initialization code example
Example 1: how to initialize an struct object in c++
struct Coordonnees
{
int x;
int y;
}
int main()
{
Coordonnees coords = {1,2};
}
Example 2: 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,
};
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: structs in c
#include <stdio.h>
#include <stdlib.h>
struct book{
char name[50];
char author[50];
char ISBN[11];
};
int main(){
struct book book1;
strcpy(book1.name, "james and the giant tatti");
strcpy(book1.author, "Krishan Grewal");
strcpy(book1.ISBN, "12345678987");
printf("book name: %s\n", book1.name);
printf("book author: %s\n", book1.author);
printf("book ISBN: %s\n", book1.ISBN);
return 0;
}
Example 5: instance for c in struct
typedef struct listitem listitem;