array as parameter of a function

后端 未结 3 834
旧巷少年郎
旧巷少年郎 2021-01-24 05:09

There is a array of structures.

    static field fields[xsize][ysize];

I want to change it in function

    void MoveLeft(pacma         


        
3条回答
  •  死守一世寂寞
    2021-01-24 05:34

    Although arrays and pointers are somewhat interchangeable in C, they're not exactly the same. In particular, an array of arrays and an array of pointers are laid out differently in memory.

    Here's a way to make an array of pointers which refers to the same data as your existing array of arrays:

    field* field_rows[xsize];
    for (unsigned int i=0; i

    Then a pointer to that field_rows array can be passed to MoveLeft:

    MoveLeft(&Pacman,field_rows,play);
    

    Another solution might be to change the declaration of MoveLeft instead, to take a pointer to array of arrays:

    void MoveLeft(pacman *Pacman, field fields[xsize][ysize], int **play);
    
    MoveLeft(&Pacman,fields,play);
    

提交回复
热议问题