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;
}
cellXandcellYasint *, but you do not allocate any memory to be pointed at by those pointers. Where do you think*grid[x][y].cellXpoint to in the first double loop?