0
I want to save and recover data from a struct
, but this struct
has other struct inside it, I don’t know if I did it correctly, I don’t know if I should use typedef
or just struct
.
I don’t know if I should wear one malloc()
for each sub struct
, or the first already calculates all the required space.
Mesas = malloc(sizeof(stTable));
ou
Mesas->PlayersList[0] = malloc(9*sizeof(stPlayer));
...
I also need that struct stTable *Mesas;
be a array with 100 positions.
to use type:
Mesas[99]->Flop->Card01 = 10;
Mesas[1]->Flop->Card01 = 2;
ou (qual a diferença)
Mesas[99].Flop.Card01 = 10;
Mesas[1].Flop.Card01 = 2;
typedef struct stCard {
BYTE carta;
BYTE pinta;
}stCard;
typedef struct stHand {
stCard* Card01;
stCard* Card02;
}stHand;
typedef struct stFlop {
stCard* Card01;
stCard* Card02;
stCard* Card03;
}stFlop;
typedef struct stTurn {
stCard* Card01;
}stTurn;
typedef struct stRiver {
stCard* Card01;
}stRiver;
typedef struct stPlayer {
DWORD Id;
char Nome[250];
DWORD BaseAddr;
DWORD Fichas;
DWORD Bet;
DWORD LastBet;
stHand* Mao;
}stPlayer;
typedef struct stTable {
BYTE Id;
DWORD BaseAddr;
DWORD LastHand;
DWORD CurrentHand;
DWORD Ante;
DWORD SmallBlind;
DWORD BigBlind;
stPlayer* PlayersList[9];
stFlop* Flop;
stTurn* Turn;
stRiver* River;
}stTable;
struct stTable *Mesas;
void init_t()
{
Mesas = malloc(sizeof(stTable));
Mesas->Id = 100;
}
I’m used to programming in java POO, now I have a job to do in C and I’m having difficulty understanding this, well, let me try to explain to you better what I’m trying to do.
– Victor Costa
in java if you create a Table class, within this class I can put my variables, gets, sets, even these variables can be another class, that’s where my idea of creating a struct inside another struct comes from, I understand the pointer concept, I don’t think I need these pointers, I’ll rewrite the code and edit the question, if possible read it again and try to help me. i just want to save my data inside a struct like:(sets) table[0]. Playerslist[2]. Name = "Joao"; table[1]. Playerlist[3]. Name = "Maria"; and retrieve values (gets) table[0]. Playerlist[2]. Name;
– Victor Costa
should declare the structure typedef struct stPlayer and use stPlayer , or declare the structure struct stPlayer and use struct stPlayer
– Victor Costa
Even in Java this modeling would be wrong, but in C more wrong still, you can’t try to do Java in C. And I’m not even going to say that Java already induces people to do a lot of wrong and the most wrong of all is to do things without even understanding why they’re doing it. You can’t rephrase the question, I answered what was asked, change the question invalidates my answer. You asked a question and I answered it. If you have another question you have to ask another question, it is answered. If it doesn’t solve the problem completely it’s just because the question isn’t complete, I can only answer it.
– Maniero
already marked as correct, you can read again and help me:
– Victor Costa
If you’re doing this project to learn, go ahead. If it’s a program to use, better to use a higher-level language than C - it could be Java. You have to take care of a lot more detail - and all critical, than with a language that can already at least create and decrypt objects on its own.
– jsbueno
@Victorcosta as I said, this question is answered, if there are others ask new question with your problem now, about the use of
malloc()
. I repeat, this problem is poorly defined even for Java, it does not seem that it should be so, in C it makes less sense yet. Looks like it should be at most 3structs
(in Java would be 3 classes) and should allocate nothing but the table, and look there (remembering that I am speculating). When you model wrong the entire application is doomed to make one mistake after another. And that’s not how you learn C, it’s not skipping steps because you "know" Java.– Maniero
I have already solved, thanks, and yes I need to allocate memory, because I am working with hook in another application, I used calloc, which already allocates the amount I need for the array, and really did not need more pointers, only the one on the table.
– Victor Costa