Is it possible to make an array size of 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:)
-
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:)
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
-
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
OK, thanks sj:)
-
OK, thanks sj:)
The same is true in the general case an array of size N you can index only N elements 0 .. N-1. John
-
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:)
-
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:)
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
-
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
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
-
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
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]
throughbData[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" -
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]
throughbData[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"