简体   繁体   中英

C how to group structs in one array

enum state{READY, RUNNING, HALTED}
enum completeMove{COMPLETE, INCOMPLETE}
struct bot {
    int botNumber;
    char processName[MAX_PROCESSNAME_LEN];
    enum state status;
    enum completeMove completeM;
}bots[MAX_BOTS];

For example I have a structure like that. And I populate this array through a text file, where the same bot (identified by its number) will get a series of instructions. Say the text file looks like this:

1 move 2000

1 halt

If I run this file through and populate the array bots, I will get two entries, bots[0] will tell bot 1 to move 2000 units forward, and bots[1] will tell bot 1 to shut down BUT both of them will have the completeM set to INCOMPLETE. Here's the problem:

The two entries in the array each have their own states, which shouldn't be the case, my array bots is treating 1 move 2000 and 1 halt as two different bots, each with their own status. The question is how do I make it so the array can recognise the fact that the two instructions are tied to the same bot.

I need this because the program should not end until all bots have the status HALTED, but the first entry can never be HALTED because it doesn't calls for it. (if an entry has processNAME HALTED, it goes to a seperate function, and that function changes the status of the bot to HALTED).

I thought of putting everything with botNumber 1 into an entry of another array that sits on top of the existing array of bots but that seems over kill, any pros have a better solution?

I think your main problem is a conception problem, you are mixing bot with process in the same structure.

You said it : "The two entries in the array each have their own states, which shouldn't be the case"

Try to separate it into two different structure, the bot one will have bot infos and an array of the process structure, and the process structure will have info like its name or if it's completed.

Something like :

#define MAX_BOTS 10
#define MAX_PROCESS 50

#define PROCESS_NAME_SIZE 256

enum state{READY, RUNNING, HALTED};
enum completeMove{COMPLETE, INCOMPLETE};
struct bot {
    int botId; // cf botNumber
    enum state status ; // status of the bot : botId

    struct {
      char processName[PROCESS_NAME_SIZE] ;
      enum completeMove completeM;
    } process [MAX_PROCESS];
}bots[MAX_BOTS];

may be easier to manipulate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM