多维数组和指针

时间:2014-12-09 19:48:43

标签: c arrays pointers

我有一个需要修改SDL_Rect类型的二维数组中的值的函数。 因此在原型中提出了这个论点:

SDL_Rect array[DIM1][DIM2];

void function(SDL_Rect *array[][DIM2]);

但是当以这种方式调用函数时:

function(array);

我收到了一个不兼容的指针类型警告。

如果你能告诉我混音在哪里会很棒。 下面的代码是一些实际的东西,wallPosition是数组,blitLevel是函数。

Main.c:

#include <stdlib.h>
#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>

#include "movements.h"
#include "mapping.h"

#define WINDOW_HEIGHT 408
#define WINDOW_WIDTH 408
#define V_B 12                    // V_B : VERTICAL_BLOCKS
#define H_B 12                    // H_B : HORIZONTAL_BLOCKS






int main (int argc , char* argv[])
{
    int noEvent = 1;
    SDL_Surface *screen = NULL , *mario = NULL;
    SDL_Rect marioPosition , wallPosition[H_B][V_B];
    SDL_Event event;

    marioPosition.x = 68;
    marioPosition.y = 0;

    if(SDL_Init(SDL_INIT_VIDEO) == -1)
    {
        fprintf(stderr , "SDL initialization error");
        exit(EXIT_FAILURE);
    }

    SDL_WM_SetIcon(IMG_Load("images/mario_down.gif") , NULL);

    screen = SDL_SetVideoMode(408 , 408 , 32 , SDL_HWSURFACE | SDL_DOUBLEBUF);
    if(screen == NULL)
    {
        fprintf(stderr , "Unable to load video mode");
        exit(EXIT_FAILURE);
    }

    SDL_FillRect(screen , NULL , SDL_MapRGB(screen->format , 0 , 0 , 0));

    SDL_WM_SetCaption("Mario Sokoban (Yep)" , NULL);

    mario = IMG_Load("images/mario_down.gif");

    SDL_BlitSurface(mario , NULL , screen , &marioPosition);
    blitLevel(screen , wallPosition);

    SDL_Flip(screen);

    SDL_EnableKeyRepeat(10, 5);

    while(noEvent)
    {
        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
                noEvent = 0;
                break;

            case SDL_KEYDOWN:
                moveMario(&mario , event , &noEvent , &marioPosition , wallPosition);
                break;

        }

        SDL_FillRect(screen , NULL , SDL_MapRGB(screen->format , 0 , 0 , 0));
        blitLevel(screen , wallPosition);
        SDL_BlitSurface(mario , NULL , screen , &marioPosition);

        SDL_Flip(screen);
    }



    SDL_Quit();

    return EXIT_SUCCESS;
}

mapping.c:

#include <stdlib.h>
#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>

#include "movements.h"
#include "mapping.h"

#define WINDOW_HEIGHT 408
#define WINDOW_WIDTH 408
#define V_B 12                    // V_B : VERTICAL_BLOCKS
#define H_B 12                     // H_B : HORIZONTAL_BLOCKS

void blitLevel(SDL_Surface *screen , SDL_Rect *wallPosition[][V_B])
{
    int i = 0 , j = 0 , v = 0 , w = 0;

    FILE *mapFile = NULL;

    SDL_Surface *wall = NULL;

    wall = IMG_Load("images/wall.jpg");

    mapFile = fopen("mapping.txt" , "r");

    if(mapFile != NULL)
    {
         for(i = 0 ; i <= 408 ; i += 34)
            {
                for(j = 0 ; j <= 408 ; j += 34)
                    {
                        wallPosition[v][w]->x = i;
                        wallPosition[v][w]->y = j;

                        v ++;
                        w ++;

                        if(fgetc(mapFile) == '1')
                            SDL_BlitSurface(wall , NULL , screen , *wallPosition);
                    }
            }
    }

    fclose(mapFile);
}

1 个答案:

答案 0 :(得分:1)

void function(SDL_Rect *array[][DIM2]);

此函数将指向SDL_Rect的指针数组作为参数。所以它与您传递的数组不同,其中每个元素都是SDL_Rect,而不是SDL_Rect*

正确的原型是void function(SDL_Rect array[][DIM2]);

C FAQ可能对您有所帮助。