lundi 9 mars 2020

Copy Structure with 2D Array Pointer in C++ - Pointer Arithmetic

The following code doesnt allow the pointer to be accessed as a 2D Array with the notation i have used in class. Am i missing something here ? Src and Dst are allocated memory outside this func.

/* Map is a 2 Dimensional Array width = x and height = y */
/* if value of loc in src is 32 do not copy to dest */
struct Map {
   int *loc;
   int x; /
   int y;
}

/* Copy Map from Source to Dest */ 
void copyMap(Map *src, Map *dst)
{
    for (int i=0 ; i < src->x; x++)
    {
        for (int j=0 ; j < src->y; y++)
        {
            // loc is 32 skip save to dest 
            if (src->loc[i][j] != 32)
            {
              dest->loc[i][j] = src->loc[i][j]; // ==> Cant i access the pointer as 2D Array
            }

            return;
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire