Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Don't steal my stuff btw I will find you
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct
- {
- int rows, columns;
- } TableDimensions;
- void ** createTable(int rows, int columns, int elementSize)
- {
- void ** table = malloc(rows * sizeof(void *));
- *table = malloc(columns * elementSize + sizeof(TableDimensions));
- for(int i = 1; i<rows; i++)
- {
- *(table + i) = malloc(columns * elementSize);
- }
- TableDimensions test = {rows, columns};
- **((TableDimensions **)table) = test;
- *table += sizeof(TableDimensions);
- return (void **)table;
- }
- int getRows(void ** table)
- {
- return ((TableDimensions *)((*table) - sizeof(TableDimensions)))->rows;
- }
- int getColumns(void ** table)
- {
- return ((TableDimensions *)((*table) - sizeof(TableDimensions)))->columns;
- }
- void freeTable(void ** table)
- {
- free(*table - sizeof(TableDimensions));
- table = NULL;
- return;
- }
- int main(void)
- {
- int rows = 10, columns = 10;
- int ** table = (int **)createTable(rows, columns, sizeof(int));
- printf("The array values are:\n");
- // Initialize array values and print them at the same time
- int counter = 0;
- for(int i = 0; i<getRows((void **)table); i++)
- {
- for(int j = 0; j<getColumns((void **)table); j++)
- {
- counter++;
- table[i][j] = counter;
- printf("%d\t", table[i][j]);
- }
- puts("");
- }
- freeTable((void **)table);
- puts("Program has finished.");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement