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

标签 c arrays

我在使用 C 时遇到一些问题。

我的任务是:

Create a dynamically allocated matrix (max 5x5), let the user fill in the values, then send it to a function along with an unallocated array (1D) which will be used to store all values of the matrix that are lower than the average of each row. The 1D array should be defined in the main function and allocated inside the function that finds the desired elements.

一切顺利,我制作了矩阵,并将其发送到上述函数,但我无法更改新分配的一维数组的值。


#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) 但它什么也没做...

最佳答案

你必须换行

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

通过

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

因为[]优先于*

参见:Operator priorities in c/c++

关于无法在函数调用中更改动态分配的数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51986704/

相关文章:

c - 匿名字符串文字效率低下吗?

c++ - 为什么当您尝试使用 arr = {values} 为数组赋值时会发生错误?

java - 在 Java 中使用静态变量或方法作为常量数组

c - sem_t的结构和信号量的实现

C - mkfifo , ls - 不要停止

javascript - 你能用 $parse 推送到 Angular 中动态声明的数组吗?

java - 复制数组——初始大小重要吗?

java - 使用 Stream 而不是 for 循环创建增量 int 数组

c - 在 C 中将 makefile 与多个文件一起使用时遇到问题

c++ - 如何使用静态变量来记住最后传递的参数