fork download
  1. #include <stdio.h>
  2. #include "stdlib.h"
  3. #define ARRAY_IDX(type, array, i) ((type *)(array+i)) // you can only modify this line!
  4.  
  5. int main(int argc, const char * argv[]) {
  6. void *ptr = malloc(10*sizeof(int));
  7. #ifdef ARRAY_IDX
  8. for (int i = 0; i < 10; i++) {
  9. ARRAY_IDX(int, ptr, i) = i * 2;
  10. }
  11.  
  12. for (int i = 0; i < 10; i++) {
  13. printf("%d ", ARRAY_IDX(int, ptr, i));
  14. }
  15. free(ptr);
  16. #else
  17. printf("Implement ARRAY_IDX first");
  18. #endif
  19. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function 'main':
prog.c:9:32: error: lvalue required as left operand of assignment
         ARRAY_IDX(int, ptr, i) = i * 2;
                                ^
prog.c:13:16: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
         printf("%d ", ARRAY_IDX(int, ptr, i));
                ^
stdout
Standard output is empty