Referencing array of struct instances

Hello,
I am being presented with a simple issue while trying to program a game with the SDL library. I have a function that fills an instance of a structure "tile" with values. I also have a function that puts the tile on the screen.

1
2
3
tile test_tile;
create_tile(&test_tile,"res/groundtiles.bmp",0,0,321,389,16,16);
draw_tile(screen,&test_tile);


The above code works and places a single tile on the screen. My goal is to be able to create an array of tiles that work the exact same way.

I tried:

1
2
3
tile test_tile[100];
create_tile(&test_tile[0],"res/groundtiles.bmp",0,0,321,389,16,16);
draw_tile(screen,&test_tile[0]);


I ended up getting a status 3 (Me pointing to something that doesn't exist). I would appreciate it if somebody tell me what to do to make my functions treat arrays the same way as single instances. Thanks so much in advance.
Did you initialize your array of 100 ? tile test_tile[100] ? Your tile I presume is a class has default constructor ?
My apologies,
I am using c, not c++ so this is a structure with no constructor.
I try below code with initialization it is ok. No initialization it gave garbage.

1
2
3
4
5
6
7
8
9
10
11
void create_tile(int *tile) {
  cout << *tile << "\n";
}

int main(int argc,char** argv) {
  int i = 7;
  create_tile(&i);
  int j[5] = {10,11,12,13,14}; //need initialize
  create_tile(j+3);
  return 0;
}


Since it is working for int primitive type, can I see your declaration for tile struct ?
Here's what I have:

Tile Structure:
1
2
3
4
5
6
7
8
typedef SDL_Rect* Rect_Pointer;

typedef struct
{
    SDL_Surface *image;
    Rect_Pointer src;
    Rect_Pointer dest;
}tile;


Create Tile Function:
1
2
3
4
5
6
7
8
9
10
11
12
13
void create_tile(tile* Tile, const char* SRC,Sint16 SRC_x,Sint16 SRC_y, Sint16 DEST_x,Sint16 DEST_y, Uint16 w, Uint16 h)
{
    Tile->image = SDL_LoadBMP(SRC);

    Tile->src->x = SRC_x;
    Tile->src->y = SRC_y;
    Tile->src->w = w;
    Tile->src->h = h;
    Tile->dest->x = DEST_x;
    Tile->dest->y = DEST_y;
    Tile->dest->w = w;
    Tile->dest->h = h;
}


I suppose you should note the the SDL_Rect structure contains four ints x,y,w,h but that is part of the library.
Which line inside create_tile function give error ? You say it works for &test_tile but not working for &test_tile[0] correct ?
Last edited on
Every line gives the error. It seems to be an obvious error that stems from passing the reference to the instance.

Edit:
This is a crappy solution but it seems to work:
1
2
3
4
5
6
    tile temp;
    tile test_tile[100];
    create_tile(&temp,"res/groundtiles.bmp",0,0,321,389,16,16);
    test_tile[0].dest = temp.dest;
    test_tile[0].src = temp.src;
    test_tile[0].image = temp.image;


Still... any better ideas out there?
Last edited on
I'm not sure about the order of operations here, so did you try using &(tile[0]) as your first parameter?
Yeah. It gave me the same problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
typedef struct
{
    int *image;
    int src;
    int dest;
} tile;

void create_tile(tile *tile) {
  cout << tile->src << "\n";
}

int main(int argc,char** argv) {
  tile i;
  create_tile(&i);
  tile j[5];
  create_tile(&j[0]);
  return 0;
}


I try to create as close as possible to your tile struct and it is ok. It is just the values are garbage but no error in running and compiling.

Your errors is in compiling or upon running give you wrong values ?
Upon running. I'm thinking of making a typedef for the *image so it'll be of type image_pointer instead of a pointer to type image.

Edit: That had bad results, it doesn't even work with the 1st instance anymore

Edit: I am really worried now... The test_tile pointer doesn't work when I remove the array but the temp tile does. How can I remove the memory that my program left behind?
Last edited on
Can you show the error so we can have some clue? You try with &test_tile ok but with &test_tile[0] it fail so strange.
Process terminated with status 3 (0 minutes, 2 seconds)

Edit: AHA!!! New problem: I can only have 1 insance of tile or I get errors.

This works:
1
2
3
tile test_tile;
tile temp;
create_tile(&test_tile,"res/groundtiles.bmp",0,0,321,389,16,16);


This does not:
1
2
3
tile temp;
tile test_tile;
create_tile(&test_tile,"res/groundtiles.bmp",0,0,321,389,16,16);
Last edited on
Put a printf in the first statement of create_tile.

e.g

void create_tile(...) {
printf("come inside here?\n");
//can also print out the function arguments in here

}
I can't printf properly with this library but I think the real problem can be found in my last comment. Sorry, I have to pack it in for the night. I will probably resurrect this problem tomorrow at some point.
Last edited on
Topic archived. No new replies allowed.