无法在函数调用中更改动态分配的数组值

时间:2018-08-23 13:16:18

标签: c arrays

我的C出现了一些问题。

我被分配了这项任务:

  

创建一个动态分配的矩阵(最大5x5),让用户填写值,然后将其与未分配的数组(1D)一起发送到函数,该数组将用于存储低于该值的所有矩阵值每行的平均值。   一维数组应在主函数中定义,并在发现所需元素的函数中分配。

一切正常,我制作了矩阵,将其发送到上述函数,但是无法更改新分配的1D数组的值。


#include<stdio.h>
#include<stdlib.h>

int lower_than_average(int **matrix, int **p, int m, int n) {
    int i, j, sum;
    float average;
    int count = 0, counter = 0;
    for (i = 0; i < m; i++) {
        sum = 0;

        for (j = 0; j < n; j++) sum += matrix[i][j];
        average = (float)sum / n;

        for (j = 0; j < n; j++){
            if (matrix[i][j] < average) count++;
        }
    }

    *p = (int*)malloc(count * sizeof(int));

    for (i = 0; i < m; i++) {
        sum = 0;
        for (j = 0; j < n; j++) sum += matrix[i][j];
        average = (float)sum / n;
        for (j = 0; j < n; j++) {
            if (matrix[i][j] < average) *p[counter++] = matrix[i][j];
        }
    }

    printf("\n\n");

    return(count);
}

void print_matrix(int **matrix, int m, int n) {
    int i, j;
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n\n");
    }
}

int main(void) {
    int m, n, **matrix, *elements_a = NULL;
    int i, j, elements_a_size;

    do {
        printf("Enter the number of rows and columns [max 5x5]: ");
        scanf("%d %d", &m, &n);
    } while (n > 5 || n < 0 || m > 5 || m < 0);

    matrix = (int**)malloc(m * sizeof(int*)); // Alokacija redaka

    /*

        X | <empty>
        X | <empty>
        X | <empty>
        X | <empty>

    */

    for (i = 0; i < m; i++) matrix[i] = (int*)calloc(n, sizeof(int)); // Alokacija stupaca

    /*

        X | Y Y Y Y Y Y
        X | Y Y Y Y Y Y
        X | Y Y Y Y Y Y
        X | Y Y Y Y Y Y

    */

    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            printf("Enter [%d][%d] element: ", i, j);
            scanf("%d", &matrix[i][j]);
        }
    }

    printf("\n\n");

    print_matrix(matrix, m, n);

    elements_a_size = lower_than_average(matrix, &elements_a, m, n);

    printf("\nElements of rows smaller than the row average value: ");
    for (i = 0; i < elements_a_size; i++) printf("%d ", elements_a[i]);

    for (i = 0; i < m; i++) free(matrix[i]);
    free(matrix);

    return 0;
}

对于输入:3 3然后1 2 3 4 5 6 7 8 9 它实际上什么也不输出。

**p and *p[0] 

代替

*p[counter++]

它输出以下内容:7 -2467754 -2467754

我在做什么错?我以为分配是正确的,并且我可以访问*(p + n),但是它什么也没做...

2 个答案:

答案 0 :(得分:3)

您必须更改行

 *p[counter++] = matrix[i][j];

作者

 (*p)[counter++] = matrix[i][j];

因为[]优先于*

请参阅:Operator priorities in c/c++

答案 1 :(得分:0)

最简单的方法可能是创建一个足以将所有元素存储在数组中的一维数组,然后在浏览2D数组的每一行时填充1D数组。遍历2D数组后,将所需的值复制到足够长的数组中并返回。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int lower_than_average(int **matrix, int **p, int m, int n) {
    int i, j, sum;
    float average;
    int count = 0, counter = 0;

    int *buff;
    buff = (int *)malloc(m * n * sizeof(int));
    if (NULL == buff)
    {
        return (-1);  //return negative value to show an error occurred
    }

    for (i = 0; i < m; i++) {
        sum = 0;

        for (j = 0; j < n; j++) sum += matrix[i][j];
        average = (float)sum / n;

        for (j = 0; j < n; j++){
            if (matrix[i][j] < average) 
            {
                buff[count] = matrix[i][j];
                count++;
            }
        }
    }

    *p = (int*)malloc(count * sizeof(int));
    if(NULL == *p)
    {
        free(buff);
        return (-1);  //return negative value to show an error occurred
    }
    memcpy(*p, buff, count*sizeof(int));
    free(buff);

    printf("\n\n");

    return(count);
}

void print_matrix(int **matrix, int m, int n) {
    int i, j;
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n\n");
    }
}

int main(void) {
    int m, n, **matrix, *elements_a = NULL;
    int i, j, elements_a_size;

    do {
        printf("Enter the number of rows and columns [max 5x5]: ");
        scanf("%d %d", &m, &n);
    } while (n > 5 || n < 0 || m > 5 || m < 0);

    matrix = (int**)malloc(m * sizeof(int*)); // Alokacija redaka

    /*

        X | <empty>
        X | <empty>
        X | <empty>
        X | <empty>

    */

    for (i = 0; i < m; i++) matrix[i] = (int*)calloc(n, sizeof(int)); // Alokacija stupaca

    /*

        X | Y Y Y Y Y Y
        X | Y Y Y Y Y Y
        X | Y Y Y Y Y Y
        X | Y Y Y Y Y Y

    */

    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            printf("Enter [%d][%d] element: ", i, j);
            scanf("%d", &matrix[i][j]);
        }
    }

    printf("\n\n");

    print_matrix(matrix, m, n);

    elements_a_size = lower_than_average(matrix, &elements_a, m, n);

    printf("\nElements of rows smaller than the row average value: ");
    for (i = 0; i < elements_a_size; i++) printf("%d ", elements_a[i]);

    printf("\n");  //a new line so the prompt isn't on the same line as the output

    for (i = 0; i < m; i++) free(matrix[i]);
    free(matrix);
    free(elements_a);  //also free the memory dynamically allocated in the function!

    return 0;
}

结果:

Enter the number of rows and columns [max 5x5]: 3
3
Enter [0][0] element: 1
Enter [0][1] element: 2
Enter [0][2] element: 3
Enter [1][0] element: 4
Enter [1][1] element: 5
Enter [1][2] element: 80
Enter [2][0] element: 12
Enter [2][1] element: 2
Enter [2][2] element: 1


1 2 3 

4 5 80 

12 2 1 




Elements of rows smaller than the row average value: 1 4 5 2 1