Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Initialising a items in a structure

Initialising a items in a structure

Scheduled Pinned Locked Moved C / C++ / MFC
questionlounge
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Grahamfff
    wrote on last edited by
    #1

    I wist to initialise some items in a structure, but the only way I can see to do it is as follows:- // General part of the Structure (used many times) typedef struct { char fieldDesc [100]; char detailsDesc [20]; char unitsStr [10]; }GENERAL_DATA; // The main structure typedef struct { unsigned short data; // Dyanamic data GENERAL_DATA details; // Static data }ITEM_DATA; ITEM_DATA thisItem; strcpy(thisItem.details.fieldDesc,"The field description"); strcpy(thisItem.details.detailsDesc,"This is the details.."); etc Are they any way I can set the char data up in ITEM_DATA, or could I set the constant data up in a new structure:- // General Structure typedef struct { unsigned short data; char fieldDesc [100] = "This is a description"; char detailsDesc [20] = "The details...."; char unitsStr [10] = "$"; }ITEM_DATA_01; etc The above does not work, but thats what I am after! I know what data should go into the char fields in advance, but the only way I can see to load this constant data in is via a statement like strcpy(thisItem.details.fieldDesc,"The field description"); Are they any other ways to load this constant data? Graham. grahamfff

    K G S 3 Replies Last reply
    0
    • G Grahamfff

      I wist to initialise some items in a structure, but the only way I can see to do it is as follows:- // General part of the Structure (used many times) typedef struct { char fieldDesc [100]; char detailsDesc [20]; char unitsStr [10]; }GENERAL_DATA; // The main structure typedef struct { unsigned short data; // Dyanamic data GENERAL_DATA details; // Static data }ITEM_DATA; ITEM_DATA thisItem; strcpy(thisItem.details.fieldDesc,"The field description"); strcpy(thisItem.details.detailsDesc,"This is the details.."); etc Are they any way I can set the char data up in ITEM_DATA, or could I set the constant data up in a new structure:- // General Structure typedef struct { unsigned short data; char fieldDesc [100] = "This is a description"; char detailsDesc [20] = "The details...."; char unitsStr [10] = "$"; }ITEM_DATA_01; etc The above does not work, but thats what I am after! I know what data should go into the char fields in advance, but the only way I can see to load this constant data in is via a statement like strcpy(thisItem.details.fieldDesc,"The field description"); Are they any other ways to load this constant data? Graham. grahamfff

      K Offline
      K Offline
      Kosk
      wrote on last edited by
      #2

      Well you can always do typedef struct yadda { yadda() { strcpy(fieldDesc,"This is a description"); strcpy(detailsDesc ,"This is a description"); strcpy(unitsStr ,"This is a description"); } char fieldDesc [100]; char detailsDesc [20]; char unitsStr [10]; }GENERAL_DATA; Then every new GENERAL_DATA variable will be initialized.

      G 1 Reply Last reply
      0
      • G Grahamfff

        I wist to initialise some items in a structure, but the only way I can see to do it is as follows:- // General part of the Structure (used many times) typedef struct { char fieldDesc [100]; char detailsDesc [20]; char unitsStr [10]; }GENERAL_DATA; // The main structure typedef struct { unsigned short data; // Dyanamic data GENERAL_DATA details; // Static data }ITEM_DATA; ITEM_DATA thisItem; strcpy(thisItem.details.fieldDesc,"The field description"); strcpy(thisItem.details.detailsDesc,"This is the details.."); etc Are they any way I can set the char data up in ITEM_DATA, or could I set the constant data up in a new structure:- // General Structure typedef struct { unsigned short data; char fieldDesc [100] = "This is a description"; char detailsDesc [20] = "The details...."; char unitsStr [10] = "$"; }ITEM_DATA_01; etc The above does not work, but thats what I am after! I know what data should go into the char fields in advance, but the only way I can see to load this constant data in is via a statement like strcpy(thisItem.details.fieldDesc,"The field description"); Are they any other ways to load this constant data? Graham. grahamfff

        G Offline
        G Offline
        Gary R Wheeler
        wrote on last edited by
        #3

        If this is C++, you should add constructors to both struct's to handle the initialization. If this is 'plain old' C (which I guessed might be the case, from your use of typedef), you can try the following:

        // General part of the Structure (used many times)
        typedef struct
        {
        char fieldDesc [100];
        char detailsDesc [20];
        char unitsStr [10];
        } GENERAL_DATA;
        // The main structure
        typedef struct
        {
        unsigned short data; // Dyanamic data
        GENERAL_DATA details; // Static data
        }ITEM_DATA;
        static ITEM_DATA InitialItemData = {

        1234,                 // 'unsigned short data' in the ITEM\_DATA struct
        {
            "field description",    // char fieldDesc\[100\]
            "detail",               // char detailsDesc\[20\]
            "units"                 // char unitsStr\[10\]
        }
        

        };

        Anywhere you need to initialize an ITEM_DATA, just assign it:

        ITEM_DATA current_item;
        current_item = InitialItemData;


        Software Zen: delete this;

        G 1 Reply Last reply
        0
        • K Kosk

          Well you can always do typedef struct yadda { yadda() { strcpy(fieldDesc,"This is a description"); strcpy(detailsDesc ,"This is a description"); strcpy(unitsStr ,"This is a description"); } char fieldDesc [100]; char detailsDesc [20]; char unitsStr [10]; }GENERAL_DATA; Then every new GENERAL_DATA variable will be initialized.

          G Offline
          G Offline
          Grahamfff
          wrote on last edited by
          #4

          Thanks for the info, that solves the first problem. But can you advise on how to use a union or other overlay method. I just want to deal with a buffer when sending out the data, but deal with the structure itmes in the program; i.e. two names for the same item of data; see listing below typedef struct yadda { yadda() { strcpy(fieldDesc,"This is a description"); strcpy(detailsDesc ,"This is a description"); strcpy(unitsStr ,"This is a description"); } char fieldDesc [100]; char detailsDesc [20]; char unitsStr [10]; }GENERAL_DATA; typedef struct { unsigned short data; GENERAL_DATA details; }ITEM_DATA; union DATA01 { ITEM_DATA thisSignal; unsigned int buffer[512]; }data01; I get the error c:\Overlays\OverlaysDlg.cpp(48): error C2620: member 'SIGNAL1::thisSignal' of union 'SIGNAL1' has user-defined constructor or non-trivial default constructor. Are they another method of overlaying the data. grahamfff

          1 Reply Last reply
          0
          • G Gary R Wheeler

            If this is C++, you should add constructors to both struct's to handle the initialization. If this is 'plain old' C (which I guessed might be the case, from your use of typedef), you can try the following:

            // General part of the Structure (used many times)
            typedef struct
            {
            char fieldDesc [100];
            char detailsDesc [20];
            char unitsStr [10];
            } GENERAL_DATA;
            // The main structure
            typedef struct
            {
            unsigned short data; // Dyanamic data
            GENERAL_DATA details; // Static data
            }ITEM_DATA;
            static ITEM_DATA InitialItemData = {

            1234,                 // 'unsigned short data' in the ITEM\_DATA struct
            {
                "field description",    // char fieldDesc\[100\]
                "detail",               // char detailsDesc\[20\]
                "units"                 // char unitsStr\[10\]
            }
            

            };

            Anywhere you need to initialize an ITEM_DATA, just assign it:

            ITEM_DATA current_item;
            current_item = InitialItemData;


            Software Zen: delete this;

            G Offline
            G Offline
            Grahamfff
            wrote on last edited by
            #5

            Thanks for the info and yes was for C code. Just two points, how would you do it for C++ and can you overlay the structure via union; union DATA01 { ITEM_DATA thisSignal; unsigned int buffer[512]; }data01; i.e. same data by two different names. grahamfff

            G 1 Reply Last reply
            0
            • G Grahamfff

              I wist to initialise some items in a structure, but the only way I can see to do it is as follows:- // General part of the Structure (used many times) typedef struct { char fieldDesc [100]; char detailsDesc [20]; char unitsStr [10]; }GENERAL_DATA; // The main structure typedef struct { unsigned short data; // Dyanamic data GENERAL_DATA details; // Static data }ITEM_DATA; ITEM_DATA thisItem; strcpy(thisItem.details.fieldDesc,"The field description"); strcpy(thisItem.details.detailsDesc,"This is the details.."); etc Are they any way I can set the char data up in ITEM_DATA, or could I set the constant data up in a new structure:- // General Structure typedef struct { unsigned short data; char fieldDesc [100] = "This is a description"; char detailsDesc [20] = "The details...."; char unitsStr [10] = "$"; }ITEM_DATA_01; etc The above does not work, but thats what I am after! I know what data should go into the char fields in advance, but the only way I can see to load this constant data in is via a statement like strcpy(thisItem.details.fieldDesc,"The field description"); Are they any other ways to load this constant data? Graham. grahamfff

              S Offline
              S Offline
              Sheff
              wrote on last edited by
              #6

              You can use constructor in your structure like this: struct MyStructure { char blablabla[200]; MyStructure() { strcpy(this->blablabla,"Testing"); } }

              1 Reply Last reply
              0
              • G Grahamfff

                Thanks for the info and yes was for C code. Just two points, how would you do it for C++ and can you overlay the structure via union; union DATA01 { ITEM_DATA thisSignal; unsigned int buffer[512]; }data01; i.e. same data by two different names. grahamfff

                G Offline
                G Offline
                Gary R Wheeler
                wrote on last edited by
                #7

                For C++, I would add a constructor. In C++, class and struct are the same except for one point. With struct, members have public access by default, while with class, the access is private by default. This means that a struct can have a constructor, and that's the place you should initialize the values. You can initialize the union as follows:

                data01.thisSignal = ItemDataInitial;

                where ItemDataInitial is an initialized ITEM_DATA as I described in the previous post. If you use a constant initializer for the union, it must correspond to the first field in it.


                Software Zen: delete this;

                G 1 Reply Last reply
                0
                • G Gary R Wheeler

                  For C++, I would add a constructor. In C++, class and struct are the same except for one point. With struct, members have public access by default, while with class, the access is private by default. This means that a struct can have a constructor, and that's the place you should initialize the values. You can initialize the union as follows:

                  data01.thisSignal = ItemDataInitial;

                  where ItemDataInitial is an initialized ITEM_DATA as I described in the previous post. If you use a constant initializer for the union, it must correspond to the first field in it.


                  Software Zen: delete this;

                  G Offline
                  G Offline
                  Grahamfff
                  wrote on last edited by
                  #8

                  Thanks for the info. I have started to use your scheme, or a varient of it. Will use the union as follows:- typedef struct { unsigned short data1; }DATA1; typedef struct { unsigned : 4 data2; }DATA2; etc union ALL_DATA_UNION { DATA1 data1; DATA2 data2; etc unsigned short buffer[50]; }ALL_DATA; I need to transmit the buffer, but want all the data items 'overlayed' with the buffer. Dont need to initialise the union as can use memcpy to zero the buffer. I will have another structure with the details of each item of data and initialise it as per your scheme. Thanks again, but please point out if this scheme is fault. Regards, Graham grahamfff

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups