Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,943 questions

51,883 answers

573 users

How to use a struct where an allocated 2D array in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>

typedef struct Map {
    size_t xSize;     
    size_t ySize;     
    int mapData[];   
} Map;

Map *allocate(size_t xSize, size_t ySize) {
    // Allocate memory for the Map structure, including space for the 2D array data.
    Map *map = malloc(sizeof(*map) + sizeof(map->mapData[0]) * xSize * ySize);
    
    // Initialize map dimensions
    if (map) {
        map->xSize = xSize;
        map->ySize = ySize;
    }
    
    return map;
}

//  Macro to simplify accessing `mapData` as a 2D array.
#define ACCESS(map) ((int (*)[map->xSize])((map)->mapData))

int main(void)
{
    // Allocate a map with dimensions 10x20.
    Map *mp = allocate(10, 20);

    // Use the ACCESS macro to access and set an element in the mapData.
    ACCESS(mp)[3][5] = 2347;

    printf("%d\n", ACCESS(mp)[3][5]);

    free(mp);
}



/*
run:

2347

*/

 



answered May 21, 2025 by avibootz
edited May 21, 2025 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>

typedef struct Map {
    size_t xSize;     
    size_t ySize;     
    int *mapData[];   
} Map;

Map *allocate(size_t xSize, size_t ySize)
{
    // Allocate memory for the Map structure and space for row pointers.
    Map *map = malloc(sizeof(*map) + sizeof(map->mapData[0]) * ySize);

    // If allocation succeeded
    if (map) {
        map->xSize = xSize;
        map->ySize = ySize;
        
        for (size_t i = 0; i < ySize; i++)
        {
            // Allocate memory for each row with `xSize`.
            map->mapData[i] = malloc(sizeof(map->mapData[0][0]) * xSize);
            if (!map->mapData[i]) {
                puts("Memory allocation error");
                free(map);
                map = NULL;
                break;
            }
        }
    }
    
    return map;
}

void free_map(Map *map) {
    if (map) {
        // Free each dynamically allocated row.
        for (size_t y = 0; y < map->ySize; y++) {
            free(map->mapData[y]);
        }
    }
    // Free the Map structure itself.
    free(map);
}

int main(void)
{
    // Allocate a map with dimensions 10x15
    Map *m = allocate(10, 15);

    // Set an element in the mapData and print it to verify.
    m->mapData[3][8] = 2839;
    printf("%d\n", m->mapData[3][8]);

    free_map(m);
}



/*
run:

2839

*/

 



answered May 21, 2025 by avibootz

Related questions

2 answers 322 views
2 answers 177 views
1 answer 59 views
1 answer 188 views
188 views asked May 19, 2025 by avibootz
1 answer 65 views
1 answer 98 views
1 answer 112 views
...