0

This program is supposed to ask the user for two values, then generate and print a table using the two values as the number of rows and columns respectively. Each cell of the table has two values, denoted as cellX and cellY. The x-value and y-value of each cell of the table is 1 and 2 respectively.

So in short, it's a dynamic 2D array of structs. The problem is, the program seems to be skipping the last for loop, so it's not printing the contents of the array of structs. No errors were generated.

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

typedef struct          // one cell of a table holding two int values
{
    int *cellX;
    int *cellY;
} Table;

int main()
{
    char dump;
    int row, col, y, x;

    printf("Enter number of rows and columns (r,c): ");
    scanf("%d%c%d", &row, &dump, &col);

    Table **grid;
    grid = (Table **)malloc(row * col * sizeof(Table));

    for (y = 0; y < row; y++)           // assigns values to the table
    {
        for (x = 0; x < col; x++)
        {
            *grid[x][y].cellX = 1;      // all x-values will be 1
            *grid[x][y].cellY = 2;      // all y-values will be 2
        }
    }

    for (y = 0; y < row; y++)           // displays the table
    {
        for (x = 0; x < col; x++)
        {
            printf("%d, %d\t", *grid[x][y].cellX, *grid[x][y].cellY);
        }
    }

    free(grid);

    return 0;
}
1
  • 1
    You declare cellX and cellY as int *, but you do not allocate any memory to be pointed at by those pointers. Where do you think *grid[x][y].cellX point to in the first double loop? Commented Mar 13, 2020 at 16:01

1 Answer 1

2
grid = (Table **)malloc(row * col * sizeof(Table));

This is not how you allocate a 2D array, Table** is an array of pointers (Table*) to an array of Table, so you have to allocate all those separate sub-arrays.

Table **grid = malloc(sizeof(*grid) * row);
for (int y = 0; y < row; ++y)
    grid[y] = malloc(sizeof(*grid[0]) * col);

And then remember to free all those arrays as well.

In many cases this is not really wanted, so you can make a 1D array and then index it as-if 2D. For example with array[y * width + x].

Table *grid = malloc(sizeof(*grid) * row * col);
for (y = 0; y < row; y++)           // assigns values to the table
{
    for (x = 0; x < col; x++)
    {
        grid[y * col + x].cellX = 1;      // all x-values will be 1
        grid[y * col + x].cellY = 2;      // all y-values will be 2
    }
}

Also your struct contains pointers, but I don't really see why, and you never allocated them. Just store values.

typedef struct
{
    int cellX;
    int cellY;
} Table;
Sign up to request clarification or add additional context in comments.

3 Comments

True, I wanted to make the type clear, but guess better to just show the declaration.
Aside: sizeof(*grid) * row * col has an advantage over row * col * sizeof(*grid). The later (int * int * size_t) product more readily overflows than the first (size_t * int * int).
On that note I'd probably not use a signed int as user input to start with, it doesn't cause the problem in the question, but its another range validation to remember in cases that might allow an out of bounds read/write.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.