[HELP] Saving Char* Arrays in Structure.

Posts 1–8 of 8 · Page 1 of 1
[HELP] Saving Char* Arrays in Structure.
I AM HAVING PROBLEMS WILLING A STRUCTURE WITH CHAR* [] and getting them back to text. When i put it in MessageBoxA() it Compiles but then it crashed at that location. When in PrintRightText() it just dont draw Anything.



Structures HERE


[PHP]struct sItem
{
char* Name;
char** Options;
int* Value;
bool Selectable;
sItem(char* NAME, char** OPTIONS, int* VALUE, bool SELECTABLE)
{
Name=NAME;
Options=OPTIONS;
Value=VALUE;
Selectable=SELECTABLE;
}
~sItem()
{}
};

typedef struct {
char* Name;
int noItems;
int totHeight;
sItem** Items;
int Selected;
} sTab;
[/PHP]

The Adding FUNCTIONS

[PHP]
void cMenu::AddTab(char* name, bool selected)
{
TABS[noTabs]->Name = name;
TABS[noTabs]->Selected = selected;
TABS[noTabs]->noItems = noTempitems;
TABS[noTabs]->totHeight = temptotHeight;
TABS[noTabs]->Items = tempItems;
noTempitems=temptotHeight=0;
this->tempItems=(sItem **)malloc(10);
for (int i=0; i<10; i++) this->tempItems[i]=(sItem *)malloc(sizeof(sItem));
noTabs++;
totHeight=(noTabs*30);
}

void cMenu::AddItem(char* Item_Name, char** Options, int* DefaultValue)
{
tempItems[noTempitems]=new sItem(Item_Name, Options, DefaultValue, true);
noTempitems++;
temptotHeight=(noTempitems*16);
}[/PHP]

FILL THE STRUCTURES HERE
[php]char* Options[] = {"WEE", "WEE1", "WEE2"};
pMenu->AddText("TEXTXY" , "HEY IT IS WORKING");
pMenu->AddItem("Item 2", Options, &A[1]);
pMenu->AddItem("Item 3", Options, &A[2]);

pMenu->AddTab("Tab 1", true);
[/PHP]

AND NOT THE DRAW STUFFY


[PHP]for (int i=0; i<this->noTabs;i++)
{
for (int o=0; o<(this->TABS[i]->noItems) ;o++)
{
int val=(this->TABS[i]->Items[o]->Value)?(*this->TABS[i]->Items[o]->Value):0;
if (this->TABS[i]->Items[o]->Options){

char* texy = this->TABS[i]->Items[o]->Options[val];

PrintRightText(pFont,posi0+3, posi1+(o*15), 0xffccccff, texy);
}
}
}[/PHP]
guess no one here knows about this
thats cuz nobody wants to help mpgh anymore LOL
Quote Originally Posted by scimmyboy View Post
thats cuz nobody wants to help mpgh anymore LOL
I am not MPGH.. i am just a simple D3D coder .
recreating hans's ay
there are local and global variables. using local variables is not very useful cause they are gone after the function call

i said be careful with pointers in another thread. i would use memcpy/strcpy to copy the names/text values/etc into the structure.

btw:
Code:
this->tempItems=(sItem **)malloc(10);
this only allocates memory for 10 bytes not for 10 items
Good eye Gordon!

I see you try to allocate memory for them here, but this does not work. Your only returning a pointer to the allocated memory.
Code:
for (int i=0; i<10; i++) this->tempItems[i]=(sItem *)malloc(sizeof(sItem));
So all you ended up doing is saving pointers in those 10 bytes you allocated.

To delare an an array of sItem simply do it like any other data type
Code:
int* intArray;
intArray = new int[20]; //will create array of 20 ints
so for sItems it would be
Code:
sItem* itemArray;
itemArray = new sItem[some_number_here];
Now itemArray will point to that allocated memory.
I i will do that to make it little more safe, but menu Options still not working
Posts 1–8 of 8 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Need help?