Advertisement

Problem in C to assign struct to the second dimension in multidimensional array

Started by October 15, 2018 12:56 PM
4 comments, last by Sae Mu 6 years, 1 month ago

I'm a beginner in C, maybee I don't understand arrays in C because as Webdeveloper I used Languages like ASP, Python, PHP, Javascript and Actionscript it was easier.

My Goal is a two-dimensional array. First dimension: should contain the current level of the game, second dimension: object structure.
When I compile and execute it seems, the first dimension is used as a structure.

C:\_dev>gcc struct_def.c -o struct_def.exe -lmingw32
C:\_dev>struct_def.exe
result
x = 420.000000
x = 0.000000 < instead of 1200.000000
C:\_dev>


The Game is very small and I would like to hardcode all the level data. without having to load the levels dynamically and without malloc.
I want to iterate the array and refresh all the positions and making collision checks etc.
then iterate the array again to calculate wich object are inside the camera to draw.

How can I use the array as I would like it? First dimension: Level, second dimension: all the struct data?

thank's for helping ?

C ISO C98

<code>

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

struct dynamic_pfl{
  bool   player;
  int    type;
  int    pause_time;
  int    moving_dir;
  float  mspeed;
  float  hspeed;
  float  vspeed;
  float  x;
  float  y;
  float  friction;
  int    width;
  int    height;
  int    state;
};

int main(){

  int level = 5; //max levels of the game

  struct dynamic_pfl arr_dynamic_pfl[level][20] = {
  {false,1,4,2,40,0,0,420,580,0,128,64,0},
  {false,2,4,2,40,0,0,1200,580,0,128,64,0},
  {false,1,4,2,40,0,0,111,580,0,128,64,0},
  {false,2,4,2,40,0,0,2222,580,0,128,64,0},
  {false,2,4,2,40,0,0,666,580,0,128,64,0}
  };
 
  printf("result\n");
  printf("x = %f\n",arr_dynamic_pfl[0][0].x);
  printf("x = %f",arr_dynamic_pfl[0][1].x);
  return 0;
}

</code>

You don't need a two dimensional array here. You only need an (one-dimensional) array of dynamic_pfl, and you can access the structs with arr_dynamic_pf[0].x, arr_dynamic_pf[1].x and so on.

The entire struct (all the fields) are one element of the array, so you have only one dimension (the level number)

Advertisement

thank you for the answer.

I want to stick all the Gamelevel Plattform of

dynamic_pfl into one Array.

  I want something like this.

  //highest_amount_of_dynamic_pfl_in_this_game on level 6 are 20 instances
 
  //level 1 contains 5 Plattforms of dynamic_pfl;
  struct dynamic_pfl arr_dynamic_pfl[1][highest_amount_of_dynamic_pfl_in_this_game] = {
  {false,1,4,2,40,0,0,420,580,0,128,64,0},
  {false,2,4,2,40,0,0,1200,580,0,128,64,0},
  {false,1,4,2,40,0,0,111,580,0,128,64,0},
  {false,2,4,2,40,0,0,2222,580,0,128,64,0},
  {false,2,4,2,40,0,0,666,580,0,128,64,0}
  };
 
  //level 2 contains 3 Plattforms of dynamic_pfl;
  struct dynamic_pfl arr_dynamic_pfl[2][highest_amount_of_dynamic_pfl_in_this_game] = {
  {false,1,4,2,40,0,0,111,280,0,128,64,0},
  {false,2,4,2,40,0,0,111,380,0,128,64,0},
  {false,1,4,2,40,0,0,111,480,0,128,64,0}
  };
 
  //level 3 contains 4 Plattforms of dynamic_pfl;
  struct dynamic_pfl arr_dynamic_pfl[3][highest_amount_of_dynamic_pfl_in_this_game] = {
  {false,1,4,2,40,0,0,131,2180,0,128,64,0},
  {false,2,4,2,40,0,0,141,3180,0,128,64,0},
  {false,1,4,2,40,0,0,151,4180,0,128,64,0},
  {false,1,4,2,40,0,0,161,4180,0,128,64,0}
  };

 

 

A single level


struct X foo[3] = { {...}, {...}, {...} };

A 2D array needs an additional set of brackets, ie your example compiled here using


  struct dynamic_pfl arr_dynamic_pfl[5][20] = {
  { {false, 1,4,2, 40,0,0, 420,580,0, 128,64,0},
    {false, 2,4,2, 40,0,0,1200,580,0, 128,64,0},
    {false, 1,4,2, 40,0,0, 111,580,0, 128,64,0},
    {false, 2,4,2, 40,0,0,2222,580,0, 128,64,0},
    {false, 2,4,2, 40,0,0, 666,580,0, 128,64,0}
  }
  };

I also changed the "level" to 5, since my gcc compiler complained about it:


x.c: In function ‘main’:
x.c:25:10: error: variable-sized object may not be initialized
   struct dynamic_pfl arr_dynamic_pfl[level][20] = {

 

@ Alberth , thank you very much ?

This topic is closed to new replies.

Advertisement