0

I'm trying to create a function that creates a variable sized 2D funct array. I'm using the following code, which seems to work just fine on its own:

typedef struct
{
    //Starter Properties
    int TypeB;
    int TypeF;
    int TypeW;
    //Randomized Properties
    int RandB;
    int RandF;
    int RandW;
    //Derived Properties
    int Speed;
} MapTileData;

MapTileData **Map;

int i, x=5, y=5;

//Allocate Initial Space
Map = (MapTileData**)calloc(x, sizeof(MapTileData));

for(i = 0; i < x; i++)
{
    Map[i] = (MapTileData*)calloc(y, sizeof(MapTileData));
}

So the above code creates a 2D struct array. My attempts to move the code to a function have been less successful, giving segmentation faults when trying to print the array:

void CreateMap(MapTileData **Map, int xSize, int ySize)
{
    //Variables
    int i;

    //Allocate Initial Space
    Map = (MapTileData**)calloc(xSize, sizeof(MapTileData));

    for(i = 0; i < xSize; i++)
        {
            Map[i] = (MapTileData*)calloc(ySize, sizeof(MapTileData));
        }
}

Used in the code:

MapTileData **MapTile;

CreateMap(MapTile,5,5);

Any and all help is greatly appreciated!

  • If you do `void f(int i) {i = /* something */;}`, why doesn't it change `i` outside the function? – user253751 Mar 18 '16 at 03:00
  • 1
    Unrelated, but `sizeof(MapTileData)` should be `sizeof(MapTileData*)` in the first `calloc` call. – user253751 Mar 18 '16 at 03:01
  • More or less a duplicate to allocating a "string"-array, (which in fact is as "2d-array" of `char`) inside a function: http://stackoverflow.com/q/25769443/694576 – alk Mar 18 '16 at 08:02

1 Answers1

1

Function arguments are passed by value in C and modifying arguments in callee won't affect caller's local variables.

Use pointers to modify caller's local variables.

void CreateMap(MapTileData ***Map, int xSize, int ySize)
{
    //Variables
    int i;

    //Allocate Initial Space
    *Map = calloc(xSize, sizeof(MapTileData));

    for(i = 0; i < xSize; i++)
    {
        (*Map)[i] = calloc(ySize, sizeof(MapTileData));
    }
}

Usage in the code:

MapTileData **MapTile;

CreateMap(&MapTile,5,5);

Alternate way: Pass the allocated array via the return value.

MapTileData **CreateMap(int xSize, int ySize)
{
    //Variables
    MapTileData **Map;
    int i;

    //Allocate Initial Space
    Map = calloc(xSize, sizeof(MapTileData));

    for(i = 0; i < xSize; i++)
    {
        Map[i] = calloc(ySize, sizeof(MapTileData));
    }

    //Return the value
    return Map;
}

Usage in the code:

MapTileData **MapTile;

Maptile = CreateMap(5,5);

Also note that they say you shouldn't cast the result of malloc() and its family in C.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Up'ed, assuming you left out all error checking for the sake of readability ... ;-) – alk Mar 18 '16 at 08:05
  • Thank you, this worked perfectly. I read up on using pointers in functions, I think I understand it a little better now. Cheers! – Danni111111 Mar 19 '16 at 02:48