Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- void** createTable(int numRows,int numCols,int elemSize);
- int getRowSize(void**array);
- int getColSize(void**array);
- void freeTable(void**array);
- void setTable(void**arr);
- void printTable(void**arr);
- typedef struct TableElements{
- int numRows;
- int numCols;
- }Table;
- int main(void){
- void** Array = createTable(3, 6, sizeof(float));
- printf("The hidden elements are %d and %d",getRowSize(Array),getColSize(Array));
- setTable(Array);
- printTable(Array);
- printf("\nWe made it to before the freetable function.");
- freeTable(Array);
- }
- void** createTable(int numRows, int numCols, int elemSize){
- void** Array = malloc((sizeof(void*)*numRows) + (sizeof(Table))); //Create the array with the proper size, and the size of an extra hidden table.
- Array = (void**)((Table*)Array + 1);
- if(Array != NULL){
- for(int i = 0;i<numRows;i++){
- Array[i] = malloc(numCols*elemSize);
- if(Array[i] == NULL){
- int temp = i;
- printf("\nYo the array managed to null\n");
- for(int j = 0;j<temp;j++){
- free(Array[j]);
- }
- free(Array);
- }
- }
- ((Table*)Array-1)->numRows = numRows;
- ((Table*)Array-1)->numCols = numCols;
- return Array;
- }
- return NULL;
- }
- int getRowSize(void**Array){
- return ((Table*)Array-1)->numRows;
- }
- int getColSize(void**Array){
- return ((Table*)Array-1)->numCols;
- }
- void freeTable(void**Array){
- for(int j = 0;j<getRowSize(Array);j++){
- printf("pre %d\n", j);
- free(Array[j]);
- printf("post %d\n", j);
- }
- printf("Finished the FOR loop\n");
- //Array = ((void*)((Table*)Array - 1));
- printf("Moved array over successfully\n");
- free((Table*)Array - 1);
- printf("Freed the Array Successfully\n");
- }
- void setTable(void **arr)
- {
- int i, j;
- for(i = 0; i<getRowSize(arr); i++)
- {
- for(j = 0; j<getColSize(arr); j++)
- {
- *((int*)arr+j) = j;
- }
- }
- }
- void printTable(void **arr)
- {
- int i, j;
- for(i = 0; i<getRowSize(arr); i++)
- {
- printf("\nRow #%-2d - ", i);
- for(j = 0; j<getColSize(arr); j++)
- {
- printf("%d ", *((int*)arr+j));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement