Re: Returning pointers which point to an array of structures
"Robby" <Robby@discussions.microsoft.com> wrote in message
news:368107F3-C7F3-44B4-BB2F-2AF5892E8EDE@microsoft.com...
Hello Brian,
Thankyou for your post.
I tried your code but unfortunately it gave me an "Undefined Identifier
MSCB1" error!
I have no "MSCB1" type defined. I guess what you meant was and does
compile
is:
===================================================
struct MCB1* px;
switch(j)
{
case 0:
px = (struct MCB1 *)SizeMCB_X(j);
===================================================
So I guess whatever pointer is returned from SizeMCB_X function will be
assigned to a pointer of MCB1 type called px? I find it weird, that we are
re-declaring an MCB1 pointer type which will be assigned the address to
point
to eihter the MCB1, MCB2 or MCB3 structure. What I mean is, what if the
SizeMCB_X function returns a pointer that points to the MCB2 structure,
will
px contain the address of the MCB2 structure?
If so then the program will become:
=====================================Header
struct MCB1{
int a; int b; };
struct MCB1* pMCB1;
Instead:
struct MCB{ int a; int b; };
struct MCB *pMCB1;
struct MCB *pMCB2;
struct MCB *pMCB3;
struct MCB2{
int a; int b; };
struct MCB2* pMCB2;
struct MCB3{
int a; int b; };
struct MCB3* pMCB3;
//GRAPHICS MASTER INFORMATIONS FOR CURRENT FONT
struct GMI{
int x;
int CHARSINMSG;
}aGMI[3];
void *SizeMCB_X(int j) ;
===============================================Function
void TCP_LOAD_MCB_X(int j)
{
int i,k;
struct MCB1* px;
....
switch(j)
{
case 0:
px = (struct MCB1 *) = SizeMCB_X(j); //Size & create array of MCB1
structure
break;
case 1:
px = (struct MCB1 *)= SizeMCB_X(j); //Size & create array of MCB1
structure
break;
case 2:
px = (struct MCB1 *)= SizeMCB_X(j); //Size & create array of MCB1
structure
break;
}
You no longer need a switch.
Or perhaps what you wanted was:
case 0: px = pMCB1; break;
case 1: px = pMCB2; break;
case 2: px = pMCB3; break;
for(i=0;i<20;i++)
{
k=0;
px[i].ENDOFCHAR = MCB_FLASH_BUFFER[k];k++;
px[i].A2 = MCB_FLASH_BUFFER[k];k++;
px[i].A1 = MCB_FLASH_BUFFER[k];k++;
px[i].A0 = MCB_FLASH_BUFFER[k];k++;
px[i].LEADING_PIX = MCB_FLASH_BUFFER[k];k++;
px[i].TRAILING_PIX = MCB_FLASH_BUFFER[k];k++;
px[i].CHARSPACING = MCB_FLASH_BUFFER[k];k++;
px[i].CHAR_TRSP = MCB_FLASH_BUFFER[k];k++;
px[i].mCHAR_BCOLOR = MCB_FLASH_BUFFER[k];k++;
px[i].lCHAR_BCOLOR = MCB_FLASH_BUFFER[k];k++;
px[i].mTEXT_COLOR = MCB_FLASH_BUFFER[k];k++;
px[i].lTEXT_COLOR = MCB_FLASH_BUFFER[k];k++;
}
.....
}
============================================
void *SizeMCB_X(int j)
{
#define __CTNRSIZE_X ((aGMI[m].CHARSINMSG)*16)
switch (j)
{
case 0:
return (malloc(__CTNRSIZE_X*(sizeof(struct MCB1))));
break;
case 1:
return (malloc(__CTNRSIZE_X*(sizeof(struct MCB2))));
break;
case 2:
return (malloc(__CTNRSIZE_X*(sizeof(struct MCB3))));
break;
}
}
Again, no longer any need to test j, the parameter can go away entirely.
================================================
Oh! On another note, I know we can do this:
struct MCB1{
int a; int b; }zzz[3][3];
which is a two dimensional array of the MCB1 structure, right?
But I don't know if what I am about to say exists in C or C++, can we do a
two dimensional array of the MCB1 structure on the heap using malloc and
how
would we access the items in this case?
The most common way is to flatten the array into 1-D:
declaration:
struct T* p;
allocation:
p = (struct T*)malloc(rows * cols * sizeof(struct T));
indexing:
p[row * cols + col]
If cols is constant the compiler will probably optimize it away.
All suggestions appreciated
P.S. Thanks Brian for you help, and by the way your solution did compile!
--
Best regards
Robert
"Brian Muth" wrote:
switch(j)
{
case 0:
(MCB1) = SizeMCB_X(j); //Size & create array of MCB1 structure
Change to:
MSCB1* pMCB1;
switch(j)
{
case 0:
pMCB1 = (MSCB1 *) SizeMCB_X(j);
etc.
Brian