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. array of differents types (possible?)

array of differents types (possible?)

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicsdata-structureshelpquestion
9 Posts 2 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
    George Nistor
    wrote on last edited by
    #1

    Hi, Now I use a driver (first part of the code) - no changes possible. I have to use something like a message processing and because I'm unable to use an array of the internal buffers of the driver -> I use more then 10 function with the same code. here is my code I would like to use a vector of messages (message0, message1 ..). any other ideea is welcome of grouping these data types. thanks <pre>// Gateway_array.cpp : Defines the entry point for the console application. // #include "stdafx.h" typedef struct {    unsigned char signal7 : 8;    unsigned char signal6 : 8;    unsigned char signal5 : 8;    unsigned char signal4 : 8;    unsigned char signal3 : 8;    unsigned char signal2 : 8;    unsigned char signal1 : 8;    unsigned char signal0 : 8; } msgType0; typedef union {    unsigned char _c[8];    msgType0 msg0_0; } buf0; buf0   message0; typedef struct {    unsigned char signal7 : 8;    unsigned char signal6 : 8;    unsigned char signal5 : 8;    unsigned char signal4 : 8;    unsigned char signal3 : 8;    unsigned char signal2 : 8;    unsigned char signal1 : 8;    unsigned char signal0 : 8; } msgType1; typedef union {    unsigned char _c[8];    msgType1 msg0_1; } buf1; buf1   message1; /// ................................ /// UP is the Driver ..   No changes possible // X 8 times typedef struct {    unsigned char signal7 : 8;    unsigned char signal6 : 8;    unsigned char signal5 : 8;    unsigned char signal4 : 8;    unsigned char signal3 : 8;    unsigned char signal2 : 8;    unsigned char signal1 : 8;    unsigned char signal0 : 8; } msgType; typedef union {    unsigned char _c[8];    msgType msg; } type_array; type_array   Vector[2];   // i would like to use a vector like this one int main(int argc, char* argv[]) {       Vector[0] = message0; //error when using it       return 0; } </pre>

    C 1 Reply Last reply
    0
    • G George Nistor

      Hi, Now I use a driver (first part of the code) - no changes possible. I have to use something like a message processing and because I'm unable to use an array of the internal buffers of the driver -> I use more then 10 function with the same code. here is my code I would like to use a vector of messages (message0, message1 ..). any other ideea is welcome of grouping these data types. thanks <pre>// Gateway_array.cpp : Defines the entry point for the console application. // #include "stdafx.h" typedef struct {    unsigned char signal7 : 8;    unsigned char signal6 : 8;    unsigned char signal5 : 8;    unsigned char signal4 : 8;    unsigned char signal3 : 8;    unsigned char signal2 : 8;    unsigned char signal1 : 8;    unsigned char signal0 : 8; } msgType0; typedef union {    unsigned char _c[8];    msgType0 msg0_0; } buf0; buf0   message0; typedef struct {    unsigned char signal7 : 8;    unsigned char signal6 : 8;    unsigned char signal5 : 8;    unsigned char signal4 : 8;    unsigned char signal3 : 8;    unsigned char signal2 : 8;    unsigned char signal1 : 8;    unsigned char signal0 : 8; } msgType1; typedef union {    unsigned char _c[8];    msgType1 msg0_1; } buf1; buf1   message1; /// ................................ /// UP is the Driver ..   No changes possible // X 8 times typedef struct {    unsigned char signal7 : 8;    unsigned char signal6 : 8;    unsigned char signal5 : 8;    unsigned char signal4 : 8;    unsigned char signal3 : 8;    unsigned char signal2 : 8;    unsigned char signal1 : 8;    unsigned char signal0 : 8; } msgType; typedef union {    unsigned char _c[8];    msgType msg; } type_array; type_array   Vector[2];   // i would like to use a vector like this one int main(int argc, char* argv[]) {       Vector[0] = message0; //error when using it       return 0; } </pre>

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Why don't you define type_array as union of {buf1, .., buf8}? :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      G 1 Reply Last reply
      0
      • C CPallini

        Why don't you define type_array as union of {buf1, .., buf8}? :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        G Offline
        G Offline
        George Nistor
        wrote on last edited by
        #3

        Hi with a union like this doesn't work either. I get ..cannont convert buf0 to unsigned char. typedef union {    buf0   msg0;    buf1   msg1; }   type_Array; type_Array   Vector[]={message0 , message1}; if i put like this, works: unsigned char x1; unsigned char x2; type_Array   Vector[]={x1, x2}; //(this doesn't seem to be logic i should have here a vector of 8 unsigned chars) so, the problem is the compiler see message0 of type buf0, somehow cannot use union definition. msgType0 and msgType1 are user defined types.

        C 1 Reply Last reply
        0
        • G George Nistor

          Hi with a union like this doesn't work either. I get ..cannont convert buf0 to unsigned char. typedef union {    buf0   msg0;    buf1   msg1; }   type_Array; type_Array   Vector[]={message0 , message1}; if i put like this, works: unsigned char x1; unsigned char x2; type_Array   Vector[]={x1, x2}; //(this doesn't seem to be logic i should have here a vector of 8 unsigned chars) so, the problem is the compiler see message0 of type buf0, somehow cannot use union definition. msgType0 and msgType1 are user defined types.

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          Why you want to assign a unsigned char to a msgType0? It looks more reasonable something like

          msgType0 msg;
          unsigned char x = 0x20;
          msg.signal0 = x;

          :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          G 3 Replies Last reply
          0
          • C CPallini

            Why you want to assign a unsigned char to a msgType0? It looks more reasonable something like

            msgType0 msg;
            unsigned char x = 0x20;
            msg.signal0 = x;

            :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            G Offline
            G Offline
            George Nistor
            wrote on last edited by
            #5

            yes, true my problem is I cannot build the array with this code: /// Driver typedef struct {    unsigned char signal7 : 8;    unsigned char signal6 : 8;    unsigned char signal5 : 8;    unsigned char signal4 : 8;    unsigned char signal3 : 8;    unsigned char signal2 : 8;    unsigned char signal1 : 8;    unsigned char signal0 : 8; } msgType0; typedef union {    unsigned char _c[8];    msgType0 msg0_0; } buf0; buf0   message0; typedef struct {    unsigned char signal7 : 8;    unsigned char signal6 : 8;    unsigned char signal5 : 8;    unsigned char signal4 : 8;    unsigned char signal3 : 8;    unsigned char signal2 : 8;    unsigned char signal1 : 8;    unsigned char signal0 : 8; } msgType1; typedef union {    unsigned char _c[8];    msgType1 msg0_1; } buf1; buf1   message1; /// ................................ /// UP is the Driver ..   No changed possible // X 8 times // seems logic to work with this code typedef union {    buf0   msg0;    buf1   msg1; }   type_Array; type_Array   Vector[]={message0 , message1}; so, I want to build an array of message0, message1.... not other data.

            1 Reply Last reply
            0
            • C CPallini

              Why you want to assign a unsigned char to a msgType0? It looks more reasonable something like

              msgType0 msg;
              unsigned char x = 0x20;
              msg.signal0 = x;

              :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              G Offline
              G Offline
              George Nistor
              wrote on last edited by
              #6

              hi the ideea was ok. my sintax was not this code is ok: // diff types array.cpp : Defines the entry point for the console application. // #include "stdafx.h" typedef struct {    unsigned char signal7 : 8;    unsigned char signal6 : 8;    unsigned char signal5 : 8;    unsigned char signal4 : 8;    unsigned char signal3 : 8;    unsigned char signal2 : 8;    unsigned char signal1 : 8;    unsigned char signal0 : 8; } msgType0; typedef union {    unsigned char _c[8];    msgType0 msg0_0; } buf0; buf0   message0; typedef struct {    unsigned char signal7 : 8;    unsigned char signal6 : 8;    unsigned char signal5 : 8;    unsigned char signal4 : 8;    unsigned char signal3 : 8;    unsigned char signal2 : 8;    unsigned char signal1 : 8;    unsigned char signal0 : 8; } msgType1; typedef union {    unsigned char _c[8];    msgType1 msg0_1; } buf1; buf1   message1; /// ................................ /// UP is the Driver ..   No changed possible // X 8 times int main(int argc, char* argv[]) {      printf("Hello World!\n");    typedef union {    buf0   msg0;    buf1   msg1; }   type_Array; type_Array X1; type_Array X2; X1.msg0= message0; X2.msg1= message1; type_Array Vector[2]; Vector[0]= X1; Vector[1]= X2;      return 0; }

              1 Reply Last reply
              0
              • C CPallini

                Why you want to assign a unsigned char to a msgType0? It looks more reasonable something like

                msgType0 msg;
                unsigned char x = 0x20;
                msg.signal0 = x;

                :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                G Offline
                G Offline
                George Nistor
                wrote on last edited by
                #7

                hi (again what I would like to have is an array of message0, message1...) please give me a clue if any I thought I solved it.. unfortunally I cannot refer the buffer I need. I need   message0._c[0] these buffer to refer to. the only way is with sintax: Vector[0].msg0._c[0]=1; //ok   // not usefull i have to apppend .msg0 which is not usefull // diff types array.cpp : Defines the entry point for the console application. // #include "stdafx.h" typedef struct {    unsigned char signal7 : 8;    unsigned char signal6 : 8;    unsigned char signal5 : 8;    unsigned char signal4 : 8;    unsigned char signal3 : 8;    unsigned char signal2 : 8;    unsigned char signal1 : 8;    unsigned char signal0 : 8; } msgType0; typedef union {    unsigned char _c[8];    msgType0 msg0_0; } buf0; buf0   message0; typedef struct {    unsigned char signal7 : 8;    unsigned char signal6 : 8;    unsigned char signal5 : 8;    unsigned char signal4 : 8;    unsigned char signal3 : 8;    unsigned char signal2 : 8;    unsigned char signal1 : 8;    unsigned char signal0 : 8; } msgType1; typedef union {    unsigned char _c[8];    msgType1 msg0_1; } buf1; buf1   message1; /// ................................ /// UP is the Driver ..   No changed possible // X 8 times int main(int argc, char* argv[]) {      printf("Hello World!\n");    union type_Array {    buf0   msg0;    buf1   msg1; }   ; type_Array X1; type_Array X2; X1.msg0= message0; X2.msg1= message1; type_Array Vector[2]; Vector[0]= X1; Vector[1]= X2; // what i would like to refer is this buffer message0._c[0] message0._c[0]= 1; Vector[0]._c[0]=1;   //imposible to use because of refering msg0 //Vector[0].msg0._c[0]=1; //ok   // not usefull      return 0; }

                modified on Thursday, January 21, 2010 11:34 AM

                C 1 Reply Last reply
                0
                • G George Nistor

                  hi (again what I would like to have is an array of message0, message1...) please give me a clue if any I thought I solved it.. unfortunally I cannot refer the buffer I need. I need   message0._c[0] these buffer to refer to. the only way is with sintax: Vector[0].msg0._c[0]=1; //ok   // not usefull i have to apppend .msg0 which is not usefull // diff types array.cpp : Defines the entry point for the console application. // #include "stdafx.h" typedef struct {    unsigned char signal7 : 8;    unsigned char signal6 : 8;    unsigned char signal5 : 8;    unsigned char signal4 : 8;    unsigned char signal3 : 8;    unsigned char signal2 : 8;    unsigned char signal1 : 8;    unsigned char signal0 : 8; } msgType0; typedef union {    unsigned char _c[8];    msgType0 msg0_0; } buf0; buf0   message0; typedef struct {    unsigned char signal7 : 8;    unsigned char signal6 : 8;    unsigned char signal5 : 8;    unsigned char signal4 : 8;    unsigned char signal3 : 8;    unsigned char signal2 : 8;    unsigned char signal1 : 8;    unsigned char signal0 : 8; } msgType1; typedef union {    unsigned char _c[8];    msgType1 msg0_1; } buf1; buf1   message1; /// ................................ /// UP is the Driver ..   No changed possible // X 8 times int main(int argc, char* argv[]) {      printf("Hello World!\n");    union type_Array {    buf0   msg0;    buf1   msg1; }   ; type_Array X1; type_Array X2; X1.msg0= message0; X2.msg1= message1; type_Array Vector[2]; Vector[0]= X1; Vector[1]= X2; // what i would like to refer is this buffer message0._c[0] message0._c[0]= 1; Vector[0]._c[0]=1;   //imposible to use because of refering msg0 //Vector[0].msg0._c[0]=1; //ok   // not usefull      return 0; }

                  modified on Thursday, January 21, 2010 11:34 AM

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Why isn't it useful? A dirty trick would be

                  union type_Array
                  {
                  unsigned char _c[8];
                  buf0 msg0;
                  buf1 msg1;
                  } ;

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  G 1 Reply Last reply
                  0
                  • C CPallini

                    Why isn't it useful? A dirty trick would be

                    union type_Array
                    {
                    unsigned char _c[8];
                    buf0 msg0;
                    buf1 msg1;
                    } ;

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    G Offline
                    G Offline
                    George Nistor
                    wrote on last edited by
                    #9

                    isn't usefull either because I have to modify the driver. The upper section until // /// UP is the Driver ..   No changes possible // X 8 times I cannot modify! because of these individual buffers of the driver ... insted of writing a SINGLE! Processing message function - for all messaging (involving these individual buffers) (it is the same code) - I have a dozen function making same thing. Inside them I refer to buffers like message0._c[(i=1,n)]= value;   , next function message1._c[(i=1,n)]= value   etc. If i would have an ARRAY of message0, message1, I would write a single function. with my previous code I would refer these buffers with Vector[0].msg0._c[0]=1; //ok   // not usefull But how do I put in the code line section msg0?   the above line would be in a for. Maybe I was more clear explaiing my problem this time. I think the problem here is, basically I have a union inside a union.

                    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