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. Is it possible to make an array size of 1??

Is it possible to make an array size of 1??

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
9 Posts 6 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 have an array of structs and the size is [1] so there are 2 places in the array. Is there anyway that I can make an array with the size of [0] so there will only be 1 instance in it? thanks, sj:)

    J A M 3 Replies Last reply
    0
    • J johnstonsk

      I have an array of structs and the size is [1] so there are 2 places in the array. Is there anyway that I can make an array with the size of [0] so there will only be 1 instance in it? thanks, sj:)

      J Offline
      J Offline
      John M Drescher
      wrote on last edited by
      #2

      Yes when you create an array of size [1] there is only one place in the array, Array[0]. [EDIT] In a array of size 1 you may not use Array[1]. [/EDIT] John

      J 1 Reply Last reply
      0
      • J John M Drescher

        Yes when you create an array of size [1] there is only one place in the array, Array[0]. [EDIT] In a array of size 1 you may not use Array[1]. [/EDIT] John

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

        OK, thanks sj:)

        J 1 Reply Last reply
        0
        • J johnstonsk

          OK, thanks sj:)

          J Offline
          J Offline
          John M Drescher
          wrote on last edited by
          #4

          The same is true in the general case an array of size N you can index only N elements 0 .. N-1. John

          1 Reply Last reply
          0
          • J johnstonsk

            I have an array of structs and the size is [1] so there are 2 places in the array. Is there anyway that I can make an array with the size of [0] so there will only be 1 instance in it? thanks, sj:)

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

            see prev post

            1 Reply Last reply
            0
            • J johnstonsk

              I have an array of structs and the size is [1] so there are 2 places in the array. Is there anyway that I can make an array with the size of [0] so there will only be 1 instance in it? thanks, sj:)

              M Offline
              M Offline
              Michael Dunn
              wrote on last edited by
              #6

              If you want an array with size 1, do:

              struct foo
              {
              int x[1];
              };

              An array of size 1 is legal, although not entirely useful. --Mike-- "So where does that leave us? Well, it leaves us right back where we started, only more confused than before." -- Matt Gullett Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber

              T 1 Reply Last reply
              0
              • M Michael Dunn

                If you want an array with size 1, do:

                struct foo
                {
                int x[1];
                };

                An array of size 1 is legal, although not entirely useful. --Mike-- "So where does that leave us? Well, it leaves us right back where we started, only more confused than before." -- Matt Gullett Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber

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

                Michael Dunn wrote: An array of size 1 is legal, although not entirely useful. I was thinking the same thing. A int data type would hold the same data that an array of size 1 would hold. Or even a pointer to an int would do the same thing. I am curios to know, why would someone need an array of size 1? // Afterall, I realized that even my comment lines have bugs When one cannot invent, one must at least improve (in bed).-My latest fortune cookie

                R 1 Reply Last reply
                0
                • T Toni78

                  Michael Dunn wrote: An array of size 1 is legal, although not entirely useful. I was thinking the same thing. A int data type would hold the same data that an array of size 1 would hold. Or even a pointer to an int would do the same thing. I am curios to know, why would someone need an array of size 1? // Afterall, I realized that even my comment lines have bugs When one cannot invent, one must at least improve (in bed).-My latest fortune cookie

                  R Offline
                  R Offline
                  Ryan Binns
                  wrote on last edited by
                  #8

                  Toni78 wrote: I am curios to know, why would someone need an array of size 1? Some structures are designed to be a header for a larger data structure. Take this for example

                  typedef struct
                  {
                  UINT nDataSize; // The size of the data
                  BYTE bData[1]; // The actual data
                  } MyPacket;

                  Since the data can be of any length (given by nDataSize), we can't specify the size of the array. Therefore, when allocating memory, we just go

                  MyPacket *packet = (MyPacket*)malloc(nDataLength+sizeof(UINT));
                  packet->nDataSize = nDataLength;

                  We now have a block of data where the first UINT is the nDataSize field, and the rest is the bData field. We can access it as bData[0] through bData[nDataSize-1], even though the definition is only of size 1, because we have allocated memory that follows the small structure. Hope this explains it a bit more :) Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
                  Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

                  T 1 Reply Last reply
                  0
                  • R Ryan Binns

                    Toni78 wrote: I am curios to know, why would someone need an array of size 1? Some structures are designed to be a header for a larger data structure. Take this for example

                    typedef struct
                    {
                    UINT nDataSize; // The size of the data
                    BYTE bData[1]; // The actual data
                    } MyPacket;

                    Since the data can be of any length (given by nDataSize), we can't specify the size of the array. Therefore, when allocating memory, we just go

                    MyPacket *packet = (MyPacket*)malloc(nDataLength+sizeof(UINT));
                    packet->nDataSize = nDataLength;

                    We now have a block of data where the first UINT is the nDataSize field, and the rest is the bData field. We can access it as bData[0] through bData[nDataSize-1], even though the definition is only of size 1, because we have allocated memory that follows the small structure. Hope this explains it a bit more :) Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
                    Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

                    T Offline
                    T Offline
                    Toni78
                    wrote on last edited by
                    #9

                    Ryan Binns wrote: Hope this explains it a bit more Yes it does, thank you.:) // Afterall, I realized that even my comment lines have bugs When one cannot invent, one must at least improve (in bed).-My latest fortune cookie

                    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