Below there is a unfinished code for my program, at the current stage, however, I am getting errors (Xcode log: Subscripted value is not array, pointer or vector
). I suppose that it has to do with memory allocation. This error occurs in the if statement when I try to assign value of 1 to (*map[x2][y2]).exist
and in map[x1][y1] = NULL;
. Could you please show the proper way of assigning values to such variables.
Thank you in advance!
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int num;
_Bool exist;
}name;
int main(void){
name* map[10][10];
name* guy;
guy = (name*)malloc(10*sizeof(name));
int x1, y1, x2, y2;
int m, n, o;
for(m = 0; m < 10; m++){
for(n = 0; n < 10; n++){
map[m][n] = NULL;
}
}
for(o = 0; o < 10; o++){
(*(guy+o)).num = rand() % 4;
(*(guy+o)).exist = 1;
do{
m = rand() % 10;
n = rand() % 10;
}while (map[m][n] != NULL);
map[m][n] = guy + o;
}
if(map[x2][y2] == NULL){
name *map = malloc(10*10*sizeof(name));
(*map[x2][y2]).exist = 1;
map[x1][y1] = NULL;
}
return 0;
}