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. creating an array of structures

creating an array of structures

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structuresperformance
5 Posts 3 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.
  • J Offline
    J Offline
    johnstonsk
    wrote on last edited by
    #1

    I am trying to create an array of structures. The structure that I have created looks like this: struct TSimSignal { double Value[45]; int SimWriteFlag; int data_index; int DisplayReadFlag; long TimeStamp; }; My program has to write the entire structure to memory so, another program can read that chunk of memory and use the struct. Here is the array that I created: struct TSimHeader *header_ptr = new TSimHeader[1]; I then try to add the TSimSignal to the array Is this how you would doo this? I think my array is an array of pointers to the structs, but How can I create an array that will hold the TSimSignal struct? Thanks, sj:)

    M D 2 Replies Last reply
    0
    • J johnstonsk

      I am trying to create an array of structures. The structure that I have created looks like this: struct TSimSignal { double Value[45]; int SimWriteFlag; int data_index; int DisplayReadFlag; long TimeStamp; }; My program has to write the entire structure to memory so, another program can read that chunk of memory and use the struct. Here is the array that I created: struct TSimHeader *header_ptr = new TSimHeader[1]; I then try to add the TSimSignal to the array Is this how you would doo this? I think my array is an array of pointers to the structs, but How can I create an array that will hold the TSimSignal struct? Thanks, sj:)

      M Offline
      M Offline
      Manfred Staiger
      wrote on last edited by
      #2

      Use a CArray template: #include "afxtempl.h" . . . CArray arrayName; arrayName.SetSize(0,1); Then you can simly use the the memberfunctions of CArray to Add/Insert/Delete... elements of the type TSimHeader. Search MSDN for CArray for more info. Hope that helps MS

      1 Reply Last reply
      0
      • J johnstonsk

        I am trying to create an array of structures. The structure that I have created looks like this: struct TSimSignal { double Value[45]; int SimWriteFlag; int data_index; int DisplayReadFlag; long TimeStamp; }; My program has to write the entire structure to memory so, another program can read that chunk of memory and use the struct. Here is the array that I created: struct TSimHeader *header_ptr = new TSimHeader[1]; I then try to add the TSimSignal to the array Is this how you would doo this? I think my array is an array of pointers to the structs, but How can I create an array that will hold the TSimSignal struct? Thanks, sj:)

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        You can do something like:

        struct TSimSignal
        {
        double Value[45];
        int SimWriteFlag;
        int data_index;
        int DisplayReadFlag;
        long TimeStamp;
        } TSimSignalArr[10];

        or

        struct TSimSignal
        {
        double Value[45];
        int SimWriteFlag;
        int data_index;
        int DisplayReadFlag;
        long TimeStamp;
        } *TSimSignalArr;

        TSimSignalArr = new struct TSimSignal[10];

        J 1 Reply Last reply
        0
        • D David Crow

          You can do something like:

          struct TSimSignal
          {
          double Value[45];
          int SimWriteFlag;
          int data_index;
          int DisplayReadFlag;
          long TimeStamp;
          } TSimSignalArr[10];

          or

          struct TSimSignal
          {
          double Value[45];
          int SimWriteFlag;
          int data_index;
          int DisplayReadFlag;
          long TimeStamp;
          } *TSimSignalArr;

          TSimSignalArr = new struct TSimSignal[10];

          J Offline
          J Offline
          johnstonsk
          wrote on last edited by
          #4

          David, I must be doing something wrong because When I try that I get these errors: d:\.h(30) : error C2501: 'TSimHeader_arr' : missing storage-class or type specifiers d:\.h(30) : error C2040: 'TSimHeader_arr' : 'int' differs in levels of indirection from 'struct TSimHeader *' d:\.h(30) : error C2440: 'initializing' : cannot convert from 'struct TSimHeader *' to 'int' This conversion requires a reinterpret_cast, a C-style cast or function-style cast d:\.cpp(164) : error C2275: 'TSimHeader' : illegal use of this type as an expression d:\.h(20) : see declaration of 'TSimHeader' I don't know why I get the error about trying to convert a pointer to an int? struct TSimHeader { char *Name[45]; char *Unit[45]; double Min[45]; double Max[45]; int SignalCount; int SimStatus; }*TSimHeader_arr; TSimHeader_arr = new struct TSimHeader[10]; Once I have added all the data that I need to the TSimHeader struct I'm trying to put that struct in position 0. TSimHeader_arr[0] = TSimHeader; Thanks David, your always helpful. sj:)

          D 1 Reply Last reply
          0
          • J johnstonsk

            David, I must be doing something wrong because When I try that I get these errors: d:\.h(30) : error C2501: 'TSimHeader_arr' : missing storage-class or type specifiers d:\.h(30) : error C2040: 'TSimHeader_arr' : 'int' differs in levels of indirection from 'struct TSimHeader *' d:\.h(30) : error C2440: 'initializing' : cannot convert from 'struct TSimHeader *' to 'int' This conversion requires a reinterpret_cast, a C-style cast or function-style cast d:\.cpp(164) : error C2275: 'TSimHeader' : illegal use of this type as an expression d:\.h(20) : see declaration of 'TSimHeader' I don't know why I get the error about trying to convert a pointer to an int? struct TSimHeader { char *Name[45]; char *Unit[45]; double Min[45]; double Max[45]; int SignalCount; int SimStatus; }*TSimHeader_arr; TSimHeader_arr = new struct TSimHeader[10]; Once I have added all the data that I need to the TSimHeader struct I'm trying to put that struct in position 0. TSimHeader_arr[0] = TSimHeader; Thanks David, your always helpful. sj:)

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            johnstonsk wrote: TSimHeader_arr[0] = TSimHeader; Your TSimHeader variable shouldn't be the same name as the structure. That aside, once you have the members of this variable set accordingly, you need to do individual assignments to TSimHeader_arr[0], like: TSimHeader_arr[0].Min = TSimHeaderVar.Min; TSimHeader_arr[0].Max = TSimHeaderVar.Max; TSimHeader_arr[0].SignalCount = TSimHeaderVar.SignalCount; etc.

            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