0
votes

I want to make an array of struct, the problem is when to each single struct has different size of array. I tried to make an pointer to the array, but i dont get the point.

typedef struct {

    char *opcodeName;

    int   opcodeNumber;

    int   howManyOprendsNeeded;

    int   *sourceAddressingMode;

    int   *destAddressingMode;

}Opcode;
typedef enum {

  IMMEDIATE, DIRECT, INDEX, REGISTER

}ADDRESSING_MODE;
static const Opcode opcodes[] = {

        {"mov", 0, 2, {IMMEDIATE ,DIRECT , INDEX , REGISTER},{DIRECT , INDEX , REGISTER}   },
        {"cmp" ,1, 2, {IMMEDIATE ,DIRECT , INDEX , REGISTER}, {IMMEDIATE ,DIRECT , INDEX , REGISTER}},
        {"add" ,2, 2, {IMMEDIATE ,DIRECT , INDEX , REGISTER},{DIRECT , INDEX , REGISTER }    },
        {"sub" ,3, 2, {IMMEDIATE ,DIRECT , INDEX , REGISTER},{ DIRECT , INDEX , REGISTER}   },
        {"not" ,4, 1, {-1}                                   , {DIRECT , INDEX , REGISTER}  },
        {"clr" ,5, 1, {-1}                                   , {DIRECT , INDEX , REGISTER} },
        {"lea" ,6, 2, {DIRECT,INDEX}                         , {DIRECT , INDEX , REGISTER}   },
        {"inc" ,7, 1, {-1}                                   , {DIRECT , INDEX , REGISTER} },
        {"dec" ,8, 1, {-1}                                   , {DIRECT , INDEX , REGISTER} },
        {"jmp" ,9, 1, {-1}                                   , {DIRECT ,REGISTER}  },
        {"bne" ,10, 1,{-1}                                   , {DIRECT , REGISTER}  },
        {"red" ,11, 1,{-1}                                   ,  {DIRECT , INDEX , REGISTER}  },
        {"prn" ,12, 1,{-1}                                   , {IMMEDIATE ,DIRECT , INDEX , REGISTER}  },
        {"jsr" ,13, 1,{-1}                                   , {DIRECT , REGISTER}  },
        {"rts" ,14, 0,{-1}                                  , {-1}},
        {"stop",15, 0,{-1}                                  , {-1}},


};

How can i declare the array from the strcut, If i dont know how much the size is. The errors i got : 1.initialization of 'int *' from 'int' makes pointer from integer without a cast [-Wint-conversion] causing by {IMMEDIATE ,DIRECT , INDEX , REGISTER}. 2.excess elements in scalar initializer. causing by {-1}.

2
You can't initialize pointers like that. It could be possible using compound literals to make the pointer point to an array (e.g. (int[]){ ... }), but then this initialization must be at file-scope or else the array will end its life when the block it's defined in ends. - Some programmer dude
The easiest solution is to let sourceAddressingMode and destAddressingMode be arrays of a fixed maximum size. Then use as much of them as needed. They are not arrays currently, but pointers, which doesn't make sense given this initializer list you've provided. - Lundin

2 Answers

1
votes

first off all, i think when i tied to compile your code i had so much warning, if i were you i would try to avoid those, wall warning are not bugs, but some are and are a good way to debug. most errors are: incompatible integer to pointer conversion initializing 'int *' with an expression of type 'int'

One possibily that would make a lot of sense to me would be, as you know in advance thse size of your arrays to declare them this way:

int   sourceAddressingMode[4];

int   destAddressingMode[4];

but is not optimal.

The best to me would be using binary operators:

your opcode become:

typedef enum {

IMMEDIATE 0, DIRECT 1, INDEX 2, REGISTER 4

}ADDRESSING_MODE;

your sourceAddressingMode and destAddressingMode are of type int.

declaration is quite easy and looks like

{"mov", 0, 2, {IMMEDIATE | DIRECT | INDEX | REGISTER},{DIRECT | INDEX | REGISTER}   },

and to know for exemple of your first opCode have a sourceAddressingMode that is DIRECT, you can use the '&' operator such has:

if (Opcode[0].sourceAddressingMode & DIRECT)
  //do stuff

hope that will help, and i ll be glad to know what kiran Biradar does think about this solution instead of casts. cheers

1
votes

You need to treat initialization elements as compound literals if you want to assign it to pointer.

Example:

change

{"mov", 0, 2, {IMMEDIATE ,DIRECT , INDEX , REGISTER},{DIRECT , INDEX , REGISTER}   },

to

{"mov", 0, 2, (int []){IMMEDIATE ,DIRECT , INDEX , REGISTER}, (int []){DIRECT , INDEX , REGISTER}   },

Note:: (int []) will be having local scope and accessing them outside its scope is undefined behavior.