Click here to Skip to main content
15,921,643 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionIs it possible to make an array size of 1?? Pin
johnstonsk2-Jul-03 6:20
johnstonsk2-Jul-03 6:20 
AnswerRe: Is it possible to make an array size of 1?? Pin
John M. Drescher2-Jul-03 6:25
John M. Drescher2-Jul-03 6:25 
GeneralRe: Is it possible to make an array size of 1?? Pin
johnstonsk2-Jul-03 6:30
johnstonsk2-Jul-03 6:30 
GeneralRe: Is it possible to make an array size of 1?? Pin
John M. Drescher2-Jul-03 6:43
John M. Drescher2-Jul-03 6:43 
AnswerRe: Is it possible to make an array size of 1?? Pin
AlexO2-Jul-03 7:03
AlexO2-Jul-03 7:03 
AnswerRe: Is it possible to make an array size of 1?? Pin
Michael Dunn2-Jul-03 7:30
sitebuilderMichael Dunn2-Jul-03 7:30 
GeneralRe: Is it possible to make an array size of 1?? Pin
Toni782-Jul-03 12:15
Toni782-Jul-03 12:15 
GeneralRe: Is it possible to make an array size of 1?? Pin
Ryan Binns2-Jul-03 16:17
Ryan Binns2-Jul-03 16:17 
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 Smile | :)

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"

GeneralRe: Is it possible to make an array size of 1?? Pin
Toni783-Jul-03 19:25
Toni783-Jul-03 19:25 
GeneralGet the version of my Application.. Pin
RobJones2-Jul-03 6:04
RobJones2-Jul-03 6:04 
GeneralRe: Get the version of my Application.. Pin
AlexO2-Jul-03 6:13
AlexO2-Jul-03 6:13 
GeneralRe: Get the version of my Application.. Pin
RobJones2-Jul-03 6:24
RobJones2-Jul-03 6:24 
GeneralThanks.. Pin
RobJones2-Jul-03 6:47
RobJones2-Jul-03 6:47 
GeneralC++ Exceptions and Catch-All Handlers Pin
TheGlynn2-Jul-03 5:30
TheGlynn2-Jul-03 5:30 
GeneralRe: C++ Exceptions and Catch-All Handlers Pin
jhwurmbach2-Jul-03 5:41
jhwurmbach2-Jul-03 5:41 
GeneralRe: C++ Exceptions and Catch-All Handlers Pin
John M. Drescher2-Jul-03 5:46
John M. Drescher2-Jul-03 5:46 
GeneralRe: C++ Exceptions and Catch-All Handlers Pin
peterchen2-Jul-03 10:48
peterchen2-Jul-03 10:48 
GeneralHeap Pin
Ken Mazaika2-Jul-03 5:18
Ken Mazaika2-Jul-03 5:18 
GeneralRe: Heap Pin
Dean Goodman2-Jul-03 5:40
Dean Goodman2-Jul-03 5:40 
GeneralRe: Heap Pin
Ken Mazaika2-Jul-03 5:49
Ken Mazaika2-Jul-03 5:49 
GeneralRe: Heap Pin
Dean Goodman2-Jul-03 6:12
Dean Goodman2-Jul-03 6:12 
GeneralRe: Heap Pin
Ken Mazaika2-Jul-03 7:39
Ken Mazaika2-Jul-03 7:39 
GeneralRe: Heap Pin
AlexO2-Jul-03 6:57
AlexO2-Jul-03 6:57 
GeneralEditing Title Bar Captions Pin
Captain Apathy2-Jul-03 5:02
Captain Apathy2-Jul-03 5:02 
GeneralRe: Editing Title Bar Captions Pin
David Crow2-Jul-03 10:26
David Crow2-Jul-03 10:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.