0

How do I make this struct instruction visible to struct cpu state? Like If I put cpu state struct first it doesn't work because some other arguments won't be visible to struct instruction but if I put it reverse again I have a problem.

struct  cpu_state
{
    int nextinstructionlinenumber;
    char filename[200];
    char RLO;

    struct instruction instructionlist[INSTRUCTIONLIST_SIZE];
    int instructionlistnumber;
    struct variable variablelist[INSTRUCTIONLIST_SIZE];
    int variablelistnumber;

};
struct cpu_state CPU; 


struct instruction{
    char name[LINE_CHAR_NUM];
    void(*function)(struct cpu_state* pstate, struct line* pline);
};
1
  • for the struct instruction instructionlist[INSTRUCTIONLIST_SIZE]; to succeed, the compiler must at least know the size of the struct, so it must know its definition at this point. Commented Sep 19, 2021 at 0:32

1 Answer 1

1

You can create incomplete structure declarations provided they are just used for pointers. For example, the following order will work. Note that I created a dummy definition for struct variable since it was absent from the post. You can replace it with whatever you like:

struct variable {
    int     dummy_val;
};

struct cpu_state;
struct line;

struct instruction{
    char name[LINE_CHAR_NUM];
    void(*function)(struct cpu_state* pstate, struct line* pline);
};

struct  cpu_state
{
    int nextinstructionlinenumber;
    char filename[200];
    char RLO;

    struct instruction instructionlist[INSTRUCTIONLIST_SIZE];
    int instructionlistnumber;
    struct variable variablelist[INSTRUCTIONLIST_SIZE];
    int variablelistnumber;

};

struct cpu_state CPU;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.