Passing struct pointers to functions

◇◆丶佛笑我妖孽 提交于 2021-02-11 15:27:29

问题


Hey I'm having trouble understanding on how structs get passed to functions as pointers because i want to modify my structs later in my project. So I'm creating a game that has rooms and this function creates a 2d array of the world. I'm passing in struct room and struct World because I'm storing each room in a cord in World struct which is a 2d array. When i got print out each room, it doesn't print anything out and i think the problem is with my pointers.

So my first function that's in a struct array that will hold all the rooms and it initializes each room to the array. Then i try to print out each rooms number but it doesn't print anything.

Second function creates my 2d world, its gonna be 3x3 world. Which puts a room onto the World cords

struct room
{
    int roomNum;
    int creaturesTotal;
    char description[50];
    char cleanOrdirty[20];
};

int main()
{
    int MaxRooms = 8;
    int rows = 2;
    int cols = 2;
    //creaste each room
    struct room Room1;
    struct room Room2;
    struct room Room3;
    struct room Room4;
    struct room Room5;
    struct room Room6;
    struct room Room7;
    struct room Room8;
    struct room Room9;
    struct PC player;
    player.respect = 40;

    //Store them into a array
    struct room AllRooms[MaxRooms];
    AllRooms[0] = Room1;
    AllRooms[1] = Room2;
    AllRooms[2] = Room3;
    AllRooms[3] = Room4;
    AllRooms[4] = Room5;
    AllRooms[5] = Room6;
    AllRooms[6] = Room7;
    AllRooms[7] = Room8;
    AllRooms[8] = Room9;

    struct room World[rows][cols];

    createRooms(AllRooms);

    //create the world
    createWorld(AllRooms,rows,cols,World);

    struct PC cords[rows][cols];
    player = startPointPC(&player,rows,cols,&World,&cords);
}



void createRooms(struct room* AllRooms[])
{
    int MaxRooms = 8;
    int number = 1;
    for(int i = 0;i <= MaxRooms; i++)
    {
        AllRooms[i]->roomNum = number;
        number++;
    }
    for(int i = 0;i <= MaxRooms; i++)
    {
        printf("%d",AllRooms[i]->roomNum);
    }

}

//creates the 2d world
void createWorld(struct room* AllRooms[], int rows, int cols, struct room* World[rows][cols])
{
    int counter = 0;
    for(int i = 0; i <= rows;i++)
    {
        for(int j = 0; j <= cols; j++)
        {
            World[i][j] = AllRooms[counter];
            counter++;
            printf("\nWorld %d", World[i][j]->roomNum);
        }

    }
}

    struct PC startPointPC(struct PC* player,int rows, int cols, struct room* 
    World[rows][cols],struct PC* cords[rows][cols])
    {

       int x = 0;
       int y = 0;

       player->x = x;
       player->y = y;

       //cords[x][y] = player;
       printf("\nYou are starting in Room %d",World[x][y]->roomNum);

       return player;

}


回答1:


This statement

AllRooms[8] = Room9;

accesses a memory beyond the array that has elements in the range [0, MaxRooms).

Moreover such an assignment like for example this

AllRooms[0] = Room1;

can invoke undefined behavior because the object Room1 was not initialized and its integer data members can have trap values.

You created a one-dimensional array

struct room AllRooms[MaxRooms];

and passed it as an argument to the function createRooms

createRooms(AllRooms);

Arrays designator in expressions (with rare exceptions) are converted to pointers to their first elements.

So in fact this call

createRooms(AllRooms);

is equivalent to

createRooms( &AllRooms[0] );

and the argument has the type struct room *.

However you declared the function that has the parameter of the type struct room* AllRooms[] that is adjusted by the compiler to the type struct room **.

The compiler should issue an error message that there is no implicit conversion from one type of the argument to other type of the function parameter.

And moreover the function shall be declared before its usage in main.

Also within the function in loops like this

for(int i = 0;i <= MaxRooms; i++)

you are using an incorrect range of indices. The valid range is [0, MaxRooms)

So the function should be declared and defined at least the following way

void createRooms( struct room AllRooms[] )
{
   int MaxRooms = 8;
   int number = 1;
   for(int i = 0;i < MaxRooms; i++)
   {
       AllRooms[i].roomNum = number;
       number++;
    }
    for(int i = 0;i < MaxRooms; i++)
    {
       printf("%d",AllRooms[i].roomNum);
    }

 }

Also it is a bad idea to use the local variable MaxRooms of the function

   int MaxRooms = 8;

instead of passing a corresponding argument that specifies the size of the array.

Similar problems exist for the function createWorld.

It should be declared and defined like

void createWorld( struct room AllRooms[], int rows, int cols, struct room World[rows][cols])
{
    int counter = 0;
    for(int i = 0; i < rows;i++)
    {
        for(int j = 0; j < cols; j++)
        {
            World[i][j] = AllRooms[counter];
            counter++;
            printf("\nWorld %d", World[i][j].roomNum);
        }

    }
}


来源:https://stackoverflow.com/questions/59977531/passing-struct-pointers-to-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!