Luke Rossenvalden
Luke Rossenvalden

Reputation: 17

Multi dimensional arrays and pointers

I have a function that needs to modify values within a two dimension array of type SDL_Rect. The argument is presented thus in the prototype :

SDL_Rect array[DIM1][DIM2];

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

However when the function is called this way :

function(array);

I get an incompatible pointer type warning.

If you could tell me where the mix up is that would be great. The code below is some of the actual stuff, with wallPosition the array and blitLevel the function.

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);
}

Upvotes: 0

Views: 91

Answers (1)

macfij
macfij

Reputation: 3209

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

This function takes as argument a 2d array of pointers to SDL_Rect. So it is different than the array you're passing, where each element is a SDL_Rect, not a SDL_Rect*.

The correct prototype would be void function(SDL_Rect array[][DIM2]);.

This C FAQ might be helpful for you.

Upvotes: 1

Related Questions