As a part of my homework I have to make a function which gets a dynamic matrix and finds the matrix's elements which is equal to the sum of it's (i+j), also it have to return by value the array's size and return by reference the array. Actually I have finished with the size part and all the function, what I dont get it how can I send from main to the function an arr of struct (cuz its not initialized yet), and how can I update it in the function.
Thanks!
typedef struct Three { // struct for the elements
int i;
int j;
int value;
}Three;
typedef struct List { // linked list struct
Three node;
struct List *next;
}List;
Three CreateThree(int i, int j, int value);
List *CreateThreeList(int **Mat, int rows, int cols);
Three *CreateThreeArr(int **Mat, int rows, int cols, int *newsize);
int CreateArrAndList(int **Mat, int rows, int cols);
void main() {
int **mat;
int row, col, i, j, value, arr_size;
printf("Please select the number of Rows and Cols in Matrix :\n");
printf("Rows: ");
scanf("%d", &row);
printf("Columns: ");
scanf("%d", &col);
mat = (int**)malloc(row * sizeof(int));
for (i = 0; i<col; i++)
mat[i] = (int*)malloc(col * sizeof(int));
printf("Please select %d values to your matrix:\n", row*col);
for (i = 0; i<row; i++)
{
for (j = 0; j<col; j++)
{
printf("select value for [%d][%d]: ", i, j);
scanf("%d", &value);
mat[i][j] = value;
}
}
arr_size = CreateArrAndList(mat, row, col);
}
Three CreateThree(int i, int j, int value) {
Three node;
node.i = i;
node.j = j;
node.value = value;
return node;
}
List *CreateThreeList(int **Mat, int rows, int cols) {
int i, j;
List *lst = NULL;
List *currLst = lst;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
if ((i + j) == Mat[i][j]) {
List *tmp = (List*)malloc(sizeof(List));
tmp->node = CreateThree(i, j, Mat[i][j]);
tmp->next = NULL;
if (currLst == NULL) {
lst = tmp;
}
else {
currLst->next = tmp;
}
currLst = tmp;
}
}
}
return lst;
}
Three *CreateThreeArr(int **Mat, int rows, int cols, int *newsize) {
int i, j, arrIndex = 0, size = 0;
Three *arr;
arr = (Three*)malloc((rows*cols) * sizeof(Three));
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
if ((i + j) == Mat[i][j]) {
arr[arrIndex++] = CreateThree(i, j, Mat[i][j]);
size++;
}
}
}
arr = (Three*)realloc(arr,size * sizeof(Three));
*newsize = size;
return arr;
}
int CreateArrAndList(int **Mat, int rows, int cols) {
int arrSize;
Three *arr = CreateThreeArr(Mat, rows, cols, &arrSize);
List *lst = CreateThreeList(Mat, rows, cols);
return arrSize;
}