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. dynamic struct arrays

dynamic struct arrays

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
9 Posts 7 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.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    normally when i wanted a struct array i would do something like this

    typedef struct _TEST
    {
    char szString[64];
    int number;
    } TEST;

    TEST *t = (TEST*)malloc(sizeof(TEST) * 10));

    in which case i would end up with 10 elements in the struct array but say later, for example when a user adds an item to a list, i need to add another element, is there is there a way to easily do this ?

    M PJ ArendsP A 3 Replies Last reply
    0
    • A Anonymous

      normally when i wanted a struct array i would do something like this

      typedef struct _TEST
      {
      char szString[64];
      int number;
      } TEST;

      TEST *t = (TEST*)malloc(sizeof(TEST) * 10));

      in which case i would end up with 10 elements in the struct array but say later, for example when a user adds an item to a list, i need to add another element, is there is there a way to easily do this ?

      M Offline
      M Offline
      M Mehrdad M
      wrote on last edited by
      #2

      check realloc()

      A D 2 Replies Last reply
      0
      • A Anonymous

        normally when i wanted a struct array i would do something like this

        typedef struct _TEST
        {
        char szString[64];
        int number;
        } TEST;

        TEST *t = (TEST*)malloc(sizeof(TEST) * 10));

        in which case i would end up with 10 elements in the struct array but say later, for example when a user adds an item to a list, i need to add another element, is there is there a way to easily do this ?

        PJ ArendsP Offline
        PJ ArendsP Offline
        PJ Arends
        wrote on last edited by
        #3

        The easiest way is to use std::vector.


        "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!

        Within you lies the power for good; Use it!

        T 1 Reply Last reply
        0
        • M M Mehrdad M

          check realloc()

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          thank you this is exactly what i needed,

          1 Reply Last reply
          0
          • A Anonymous

            normally when i wanted a struct array i would do something like this

            typedef struct _TEST
            {
            char szString[64];
            int number;
            } TEST;

            TEST *t = (TEST*)malloc(sizeof(TEST) * 10));

            in which case i would end up with 10 elements in the struct array but say later, for example when a user adds an item to a list, i need to add another element, is there is there a way to easily do this ?

            A Offline
            A Offline
            Anonymous
            wrote on last edited by
            #5

            Yes it is. However, you need a totally new approach for this. Use a linked list. make a struct for the node like so: struct node { variable; . . . struct node *Link; }; now make a struct for the linked list like so struct list { node *First //first node in the list. void InsertNode(node *temp) //inserting at end of list, or anywhwere u like }; the first node will point to the next node, it will intern point to the next and so on. u can now use malloc to dynamically allocate mem for each node and append them at the end of the chain. remember to always keep the Link of teh last node in the chain as NULL. this is the end of the list. Please see more info on linked lists. There is too much out there for me to explain here, but this is the general format of a SINGLY LINKED LIST, but there many types. hope this has helped. you can change the functions in the list struct as per your requirements. you may add function for deleting, etc. You cannot create a dynamica array and maintain the data in it at the same time, unless you are using vb, where you can actually do this by ReDim. I hope i have answered your question:))

            A 1 Reply Last reply
            0
            • A Anonymous

              Yes it is. However, you need a totally new approach for this. Use a linked list. make a struct for the node like so: struct node { variable; . . . struct node *Link; }; now make a struct for the linked list like so struct list { node *First //first node in the list. void InsertNode(node *temp) //inserting at end of list, or anywhwere u like }; the first node will point to the next node, it will intern point to the next and so on. u can now use malloc to dynamically allocate mem for each node and append them at the end of the chain. remember to always keep the Link of teh last node in the chain as NULL. this is the end of the list. Please see more info on linked lists. There is too much out there for me to explain here, but this is the general format of a SINGLY LINKED LIST, but there many types. hope this has helped. you can change the functions in the list struct as per your requirements. you may add function for deleting, etc. You cannot create a dynamica array and maintain the data in it at the same time, unless you are using vb, where you can actually do this by ReDim. I hope i have answered your question:))

              A Offline
              A Offline
              Archer282
              wrote on last edited by
              #6

              actually it seems to work just fine the way i was doing it, and no offense, but i would like a second opinion, before doing this the way you have suggested

              S 1 Reply Last reply
              0
              • PJ ArendsP PJ Arends

                The easiest way is to use std::vector.


                "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #7

                vectors are C++ specifics, and so don't exist in the C language (but i agree, the owner of the thrad didn't precise told us that detail)...


                TOXCCT >>> GEII power
                [toxcct][VisualCalc]

                1 Reply Last reply
                0
                • A Archer282

                  actually it seems to work just fine the way i was doing it, and no offense, but i would like a second opinion, before doing this the way you have suggested

                  S Offline
                  S Offline
                  S Douglas
                  wrote on last edited by
                  #8

                  Archer282 wrote: but i would like a second opinion, before doing this the way you have suggested Archer, I had this very same question. With the help of a couple of others I started using std::vector. It works very well. Here is the thread I started Data Storage[^] Here is an article explaining Vectors[^] Hope this helps, Good Luck... ------------------------------- DEBUGGING : Removing the needles from the haystack.

                  1 Reply Last reply
                  0
                  • M M Mehrdad M

                    check realloc()

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

                    See my comment about realloc() here.


                    "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                    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