views:

55

answers:

2

Hello, I am having a confusing issue -- for me at least, this may be very simple and I am missing something. I am trying to initialize a 2D array of variable size, The array is a part of a struct. I am having no issue allocating memory for the array but when I try to assign characters to the array, or print them I receive unexpected results =/

My last attempt has shown that no matter when character I assign to the array, when I print a '[' will be printed... that was the first time I was actually able to print anything. My previous attempts returned seg faults. Here is the code, what am I missing. Thank you.

typedef struct world_map {
    char ** map_array;
    int X, Y;
} MAP_s;
//
MAP_s * map;

int init_map_array(void) {
    int i; // temp
    map = malloc(sizeof (MAP_s));
    map->X = 20; // Columns
    map->Y = 10; // Rows
    //
    map->map_array = malloc(map->Y * (sizeof (char *)));
    //
    if (map->map_array == 0) {
        printf("ERROR: out of memory!");
        return -1;
    } else {
        for (i = 0; i < map->Y; ++i) {
            map->map_array[i] = malloc(map->X * sizeof (char));
            if (map->map_array[i] == 0) {
                printf("ERROR: out of memory!");
                return -1;
            }
        }
    }
    int curr_pos_x, curr_pos_y;
    int limit_x = map->X;
    int limit_y = map->Y;
    //
    for (curr_pos_y = 0; curr_pos_y < limit_y; ++curr_pos_y) {
        for (curr_pos_x = 0; curr_pos_x < limit_x; ++curr_pos_x) {
            map->map_array[curr_pos_y][curr_pos_x] = "#";
        }
    }
    return 1;
}

int draw_map(void) {
    int curr_pos_x, curr_pos_y;
    int limit_x = map->X;
    int limit_y = map->Y;
    //
    for (curr_pos_y = 0; curr_pos_y < limit_y; ++curr_pos_y) {
        for (curr_pos_x = 0; curr_pos_x < limit_x; ++curr_pos_x) {
            printf("%c", map->map_array[curr_pos_y][curr_pos_x]);
        }
        printf("\n");
    }
}

int main(void) {
    init_map_array();
    draw_map();
    //
    printf("STRUCT: %i\n", sizeof (map));
    printf("X: %i\n", sizeof (map->X));
    printf("Y: %i\n", sizeof (map->Y));
    printf("ARRAY: %i\n", sizeof (map->map_array));
return (EXIT_SUCCESS);
}

As a side note, those 4 printf at the end all return "4", I'm fairly certain that if a struct contains 3 elements which are each 4 bytes that it should be larger than 4 bytes itself...

+1  A: 

Try

map->map_array[curr_pos_y][curr_pos_x] = '#';

instead of

map->map_array[curr_pos_y][curr_pos_x] = "#";

map->map_array[curr_pos_y][curr_pos_x] is of type char and you are assigning a string constant to it.

codaddict
Thank you, I'm an idiot -_-
Frstrtd Dsgnr
+1  A: 

Seems to work fine for me, but there is one error (somehow my GCC was "smart" enough to handle it but it's still an error):

map->map_array[curr_pos_y][curr_pos_x] = "#";

That assigns a char * (pointer to a char in the data segment) instead of a char which will of course result in weird characters. Change "#" to '#' and it should work.

Also, regarding your printfs at the end: they should look like this:

printf("STRUCT*: %lu\n", sizeof (map)); // Prints sizeof(MAP_s *) == sizeof(void *) == 4;
printf("STRUCT: %lu\n", sizeof (*map)); // Prints sizeof(MAP_s) == 16 on my system (iMac w/ Mac OS X),
        // alignment and native pointer size might give you different values.
printf("X: %d\n", map->X); // Prints the X dimension. No sizeof.
printf("Y: %d\n", map->Y); // Prints the Y dimension. No sizeof.

You can't print the size of the map->map_array as sizeof works at compile-time and can only return constants for types where the size is known at compile-time. The only way to determine the size of map_array is to save the size argument you gave to malloc in a variable.

DarkDust