You need to make a Vm with pointers to an array of bytes and so that there are convenient macros. But an error occurs:

#include <stdio.h> #define my_print 1 #define my_print_with_dec 2 //#define NEXTOP() *(next_instr)++ #define TARGET(op) \ case op: #define TARGET_NOARG(op)\ case op: #define DISPATCH() continue int next_instr[5]= {my_print,my_print,my_print,0}; int opcode=0; int oparg=0; int main(int argc, char** argv) { for(;;){ //opcode=NEXTOP(); opcode=*(next_instr)++; printf("opcode: %d",opcode); switch(opcode){ TARGET(my_print) { printf("me-me-me!\n"); DISPATCH(); } TARGET(0){ break; } } } return 0; } //<--main.cpp: In function 'int main(int, char**)': //main.cpp:39:24: error: lvalue required as increment operand // opcode=*(next_instr)++; 
  • Well, is it possible to apply ++ to an array! ... - Harry
  • Arrays are not pointers. They cannot be incremented, a separate pointer variable is needed. - HolyBlackCat

1 answer 1

All decided so :)

 #include <stdio.h> #define my_print 1 #define my_print_with_dec 2 #define NEXTOP() *(next_instr)++ #define TARGET(op) \ case op: #define TARGET_NOARG(op)\ case op: #define DISPATCH() continue int mass[5]= {my_print,my_print,my_print,0}; int opcode=0; int oparg=0; int *next_instr; int main(int argc, char** argv) { (next_instr)=mass; for(;;){ opcode=NEXTOP(); printf("opcode: %d",opcode); switch(opcode){ TARGET(my_print) { printf("me-me-me!\n"); DISPATCH(); } TARGET(0){ return 0; } } } return 0; } opcode: 1me-me-me! opcode: 1me-me-me! opcode: 1me-me-me! opcode: 0 ВЫПОЛНЕНИЕ SUCCESSFUL (общее время: 499ms)