обновление адреса массива по ссылке - PullRequest
0 голосов
/ 13 мая 2019

В качестве части моей домашней работы я должен сделать функцию, которая получает динамическую матрицу и находит элементы матрицы, равные сумме ее (i + j), также она должна возвращать по значению размер массива ивернуть по ссылке массив.На самом деле я закончил с частью размера и со всей функцией, что я не понимаю, как я могу отправить из main в функцию arr структуры (потому что она еще не инициализирована), и как я могу обновить ее в функции.

Спасибо!

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;
}

1 Ответ

0 голосов
/ 13 мая 2019
ptrdiff_t create_arr_and_list(int **mat, ptrdiff_t rows, ptrdiff_t cols, struct three **arr, struct list **lst)
{
        ptrdiff_t nmemb;

        *arr = create_three_arr(mat, rows, cols, &nmemb);
        *lst = create_three_list(mat, rows, cols);

        return nmemb;
}

Вы можете создать указатель в main (struct three *arr; и struct list *lst;), передать указатель на эти указатели (create_arr_and_list(mat, rows, cold, &arr, &lst);) и позже инициализировать их в функции.

int main(void)
{
        int **mat;
        int value
        ptrdiff_t row, col, nmemb;
        struct three *arr;
        struct list *lst;

        printf("Please select the number of Rows and Cols in Matrix :\n");
        printf("Rows: ");
        scanf("%ti", &row);
        printf("Columns: ");
        scanf("%ti", &col);

        mat = malloc(sizeof(*mat) * row);
        for (i = 0; i < col; i++)
                mat[i] = malloc(sizeof(*mat[i]) * col);

        printf("Please select %ti values to your matrix:\n", row * col);

        for (ptrdiff_t i = 0; i < row; i++) {
                for (ptrdiff_t j = 0; j < col; j++) {
                        printf("select value for [%ti][%ti]: ", i, j);
                        scanf("%d", &value);
                        mat[i][j] = value;
                }
        }
        nmemb = create_arr_and_list(mat, rows, cold, &arr, &lst);
}
...