·  C++ – Syntax – Collections – Struct

 

- Info:

  - Struct is collection of non-identical objects.

  - This tutorial is based on http://publications.gbdirect.co.uk/c_book/chapter6/structures.html.

 

- Example:

  - This example shows basic struct operations.

  - Create C++ Console Application Test.cpp.

 

    Test.cpp

    #include <stdio.h>

    #include <string.h>

   

    int main() {

   

      // DECLARE STRUCT PERSON AND CREATE TWO VARIABLES.

      struct person{

        int  id;

        char password[10];

      } john, jill;

   

      // CREATE TWO MORE VARIABLES OF STRUCT PERSON.

      struct person jack, lucy;

   

      // SET VALUES OF STRUCT VARIABLES.

      jack.id = 20;

      strcpy(jack.password, "pass");

   

      // GET VALUES OF STRUCT VARIABLES.

      printf("%i", jack.id      );

      printf("%s", jack.password);

   

      // COPY STRUCT VARIABLE.

      john=jack;

   

      return 1;

   

    }