未解决问题:指针作参数二重指针指针数组动态内存分配

Posted Z.L.G

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了未解决问题:指针作参数二重指针指针数组动态内存分配相关的知识,希望对你有一定的参考价值。

题目:输入m个学生n门课的成绩,计算每个学生的平均成绩,输入学生编号后输出该学生各门课的成绩。

先直接上源码:(有错误的源码)

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

 int **pointer_counterpart = NULL;

int main()
{
    void Input_number(int *m, int *n);              //输入m个学生n门课成绩
    void allocation(int m, int n);
    //void display(int **pointer, int m, int n);    
    void calculation(int **pointer, int m, int n);   //计算平均成绩
    void Search(int **pointer, int m, int n);        //查询学生成绩
    void Free(int **pointer, int m);

    int students_number, subjects_number;

    Input_number(&students_number, &subjects_number);
    allocation(students_number, subjects_number);
    //display(pointer_counterpart, students_number, subjects_number);
    calculation(pointer_counterpart, students_number, subjects_number);
    Search(pointer_counterpart, students_number, subjects_number);
    Free(pointer_counterpart, students_number);
    return 0;
}
void Input_number(int *m, int *n)
{
    printf ("Please input the numbers of students and subjects:\n");
    scanf ("%d%d", m, n);
}
void allocation(int m, int n)
{
    int *pointer[m];

    for (int i = 0; i < m; ++i) {
        pointer[i] = (int*)malloc(n * sizeof(int));
    }
    for (int i = 0; i < m; ++i) {
         printf ("Please input %d students %d grades:\n ", i + 1, n);
          for (int j = 0; j < n; ++j) {
            scanf ("%d", *(pointer + i) + j);
        }
    }
    /**完整运行完成后再回来看这里
    for (int i = 0; i < m; ++i) {
         pointer[i] = (int*)malloc(n * sizeof(int));
         printf ("Please input %d students %d grades:\n ", i + 1, n);
          for (int j = 0; j < n; ++j) {
            scanf ("%d", *(pointer + i) + j);
        }
    }*/

    /*print to try
    for (int i = 0; i < m; ++i) {
        printf ("The %d student‘s grades are:\n", i + 1);
        for (int j = 0; j < n; ++j) {
            printf ("%d ", *(*(pointer + i) + j));
        }
        printf ("\n");
    }right****/
    pointer_counterpart = pointer;
}
/*
void display(int **pointer, int m, int n)
{
    printf ("Matrix:\n");
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            printf ("%d ", *(*(pointer + i) + j));
        }
        printf ("\n");
    }
}*/
void calculation(int **pointer, int m, int n)
{
    int average;
    int sum;
    ///print to try
    printf ("%p  %p\n", pointer_counterpart, *pointer_counterpart);
    printf ("%p  %p\n", pointer, *pointer);
    ///end
    for (int i = 0; i < m; ++i) {
        sum = 0;
        average = 0;
        for (int j = 0; j < n; ++j) {
            sum += *(*(pointer + i) + j);   ///学生数 = 3, 科目数 = 3时此处算第三个平均数有错误
        }
        average = sum / n;
        printf ("%d students‘ average is %d.\n", i + 1, average);
    }
}
void Search(int **pointer, int m, int n)
{
    int num;
    bool condition = true;
    while (condition) {
        printf ("Please input the student‘s number to search his grades:\n");
        scanf ("%d", &num);
        if (num < 1 || num > m) {
            char flag;
            printf ("ERROR, reinput ‘y‘, exit ‘n‘:");
            scanf ("%c", &flag);
            if (flag == n || flag == N) {
                condition = false;
                break;
            }
            else  {
                condition = true;
                break;
            }
        }
        else {
            printf ("The %d students‘ grades are:\n", num);
            for (int i = 0; i < n; ++i) {
                //printf ("%p\n", *pointer);
                printf ("%d ", *(*(pointer + num - 1) + i));
                ///printf ("%d ", *(*(pointer_counterpart + num - 1) + i));///此处输出仍然有错误
            }
            printf ("\n");
            condition = false;
        }
    }

}
void Free(int **pointer, int m)
{
    for (int i = 0; i < m; ++i) {
        free (*(pointer + i));
        *(pointer + i) = NULL;
    }
}

第一个未解决问题:在allocation函数中动态内存分配问题

void allocation(int m, int n)
{
    int *pointer[m];

    for (int i = 0; i < m; ++i) {
        pointer[i] = (int*)malloc(n * sizeof(int));
    }
    for (int i = 0; i < m; ++i) {
         printf ("Please input %d students %d grades:\n ", i + 1, n);
          for (int j = 0; j < n; ++j) {
            scanf ("%d", *(pointer + i) + j);
        }
    }
    /**上面使用一个for先把指针数组内存分配好后,然后两个for循环输入数据是成功的。但是当如下这种分配赋值方式就有错误,错误就是在函数calculation中,当
    学生数 = 3, 科目数 = 3时,算第三个平均数有错误,calculation完整源码有标示,中至今没找出哪里有错误来。。。
    for (int i = 0; i < m; ++i) {
         pointer[i] = (int*)malloc(n * sizeof(int));
         printf ("Please input %d students %d grades:\n ", i + 1, n);
          for (int j = 0; j < n; ++j) {
            scanf ("%d", *(pointer + i) + j);
        }
    }有错误的*/

    /*print to try
    for (int i = 0; i < m; ++i) {
        printf ("The %d student‘s grades are:\n", i + 1);
        for (int j = 0; j < n; ++j) {
            printf ("%d ", *(*(pointer + i) + j));
        }
        printf ("\n");
    }right 尝试上面输入是否正确,结果没问题****/
    pointer_counterpart = pointer;
}

第二个未解决问题:

void Search(int **pointer, int m, int n)
{
    int num;
    bool condition = true;
    while (condition) {
        printf ("Please input the student‘s number to search his grades:\n");
        scanf ("%d", &num);
        if (num < 1 || num > m) {
            char flag;
            printf ("ERROR, reinput ‘y‘, exit ‘n‘:");
            scanf ("%c", &flag);
            if (flag == n || flag == N) {
                condition = false;
                break;
            }
            else  {
                condition = true;
                break;
            }
        }
        else {
            printf ("The %d students‘ grades are:\n", num);
            for (int i = 0; i < n; ++i) {
                //printf ("%p\n", *pointer);
                printf ("%d ", *(*(pointer + num - 1) + i));
                ///printf ("%d ", *(*(pointer_counterpart + num - 1) + i));///此处输出仍然有错误,当指针数组的地址传进来以后此处就是不对,脑袋要炸。。
            }
            printf ("\n");
            condition = false;
        }
    }

}

 

以上是关于未解决问题:指针作参数二重指针指针数组动态内存分配的主要内容,如果未能解决你的问题,请参考以下文章

指针做参数的动态内存分配与二重指针(上)

结构体作为函数参数值传递的问题

动态内存分配与指向它的指针变量

指针数组的动态内存分配

内存的动态分配(指针数组)

C++:delete[] 错误,指针未分配