0

I'm having difficulty passing a 2D array of structs. The size of the 2D array is dynamic (depends on given input parameters). I get the error:

maze_array.c:76:14: error: incompatible types when assigning to type ‘BlockNode {aka struct BlockNode}’ from type ‘BlockNode * {aka struct BlockNode *}’ Maze[i][j]=myBlockNode;

Here is my code:

int main(int argc, char *argv[]){   

    int MazeWidth=1;
    int MazeHeight=1;
    int NumOfAvatars=1;
    BlockNode* Maze[MazeWidth][MazeHeight];
    InitializeArray(Maze[0],MazeWidth,MazeHeight,NumOfAvatars);
    return 1;
}

int InitializeArray(BlockNode** Maze,int MazeWidth, int MazeHeight, int    NumOfAvatars){

for (int i=0; i<MazeWidth;i++)
{
    for (int j=0; j<MazeHeight;j++)
    {
        //Initialize a BlockNode
        printf("HERE1\n");
        BlockNode *myBlockNode;
        myBlockNode=calloc(1,sizeof(BlockNode));
        myBlockNode->North=0;
        myBlockNode->South=0;
        myBlockNode->East=0;
        myBlockNode->West=0;
        int myArray[NumOfAvatars];
        memset(myArray,0,sizeof(myArray));
        memcpy(myBlockNode->AvatarVisited,myArray,sizeof(myArray));

        //Place BlockNode in the Maze
        Maze[i][j]=myBlockNode;
    }
}
/*
printf("North %d\n", Maze[0][0]->North);
printf("AvatarVisted %d\n", Maze[0][0]->AvatarVisited[0]);
*/


return 1;

}
2
  • API change to int InitializeArray(int MazeWidth, int MazeHeight, BlockNode *Maze[MazeWidth][MazeHeight], int NumOfAvatars), and call as InitializeArray(MazeWidth,MazeHeight, Maze, NumOfAvatars); Commented Aug 22, 2015 at 4:02
  • 1
    That won't work, BLUEPIXY. VLAs are not somehow passed differently than any other array to a function. Commented Aug 22, 2015 at 4:29

2 Answers 2

2

You should take into account that a 2D array is not equal to pointer to pointer, for example, if you try to compile...

int array[10][10];
int** p=array;

...you would get a similar error.

If you want to pass a 2D array of pointers and use it like AnArray[i][j] = something, you should change the function declaration to...

int InitializeArray( BlockNode* Maze[][MazeHeight]
                   , int MazeWidth
                   , int MazeHeight
                   , int NumOfAvatars )

...or...

int InitializeArray( BlockNode* (*Maze)[MazeHeight]
                   , int MazeWidth
                   , int MazeHeight
                   , int NumOfAvatars )

...and call it like...

InitializeArray( Maze
               , MazeWidth
               , MazeHeight
               , NumOfAvatars );

Also, read this.

Sign up to request clarification or add additional context in comments.

Comments

1

You haven't actually asked a question, but I will assume you are seeking an explanation of the compiler error.

Within your function, Maze[i][j] will be of type BlockNode, since it is dereferencing a BlockNode ** (pointer to a pointer to a Blocknode) twice. myBlockNode is of type BlockNode *. That is the cause of the compiler error - there is no valid assignment of the form some_BlockNode = some_pointer_to_BlockNode.

The real problem, however, is that you don't properly understand the difference between pointers and arrays. You'll need to read up on such topics before anyone will be able to offer useful (that will make sense to you) advice on how to do what you want.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.