0

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;
}
  • You suppose wrong. Explain what you intend `(*map[x2][y2])` to *do* ? – WhozCraig Mar 28 '16 at 05:35
  • @WhozCraig I want to assign value of 1 to `(*map[x2][y2])` – user6115697 Mar 28 '16 at 05:36
  • @WhozCraig I am making a game, where some point should move across the field and avoid obstacles – user6115697 Mar 28 '16 at 05:38
  • 1
    Note I didn't put an assignment or value on that question. The expression `(*map[x2][y2])` itself. And you realize you're using the same identifier `map` as that found one scope-level up in `main()` right ? `name *map = ...` is declaring a variable local to that if-block scope. (and leaking the memory you allocated in the process). And fyi *neither* `x2` nor `y2` are initialized with any determinate values prior to the if-test anyway. – WhozCraig Mar 28 '16 at 05:39
  • Not exactly *leaking* - maybe even worse, *hiding*. In any function outside `main()`the original `map`will magically re-appear..... – tofro Mar 28 '16 at 07:31

1 Answers1

0

Besides the x2 and y2 not having a defined value - they do not default to 0, and the map variable in your if statement hiding the map outside your if statement, you seem to be having problems with understanding how to allocate memory as well as accessing it after it is allocated.

I suggest fixing the guy variable first and then using that experience to apply to the map variable.

The guy variable as you have it written, points to a block of memory that can hold 10 name structures. That doesn't mean that there are 10 name structures there, it just means it is a block of memory large enough to hold 10 name structures. Note that the block is not sized to hold 10 pointers to name structures but 10 actual name structures.

When you do arithmetic on the guy variable like (guy+o) you are not moving o name structures into the memory block since guy is of size pointer to name, not size of name. If pointers on your system are 4-bytes in size and the name structure is maybe 8-bytes in size then you are not moving the correct number of bytes into the block, so you are not pointed at the right place.

If you want to initialize the 10 names in guy then do yourself a favor until you are really good with pointers - create the guy variable as an array of name pointers.

So the guy definition becomes

   name* guy[10];

and the initialization of 10 names in guy becomes

   for ( m = 0; m < 10; m++ ) {
      guy[m] = malloc( sizeof( name ) );
      if ( guy[m] ) {
         guy[m]->num = rand() % 4;
         guy[m]->exist = 1;
      }
   }

instead of

guy = (name*)malloc(10*sizeof(name));

Since guy will now be recognized as a pointer to an array of name pointers, when you do arithmetic like (guy + o) it will use a pointer to a different name structure. But I would avoid that to make your life easier. Just use guy[o] to access the pointer at that location in the guy array.

You should be able to apply the same rules to map to make your code work.

T Johnson
  • 824
  • 1
  • 5
  • 10