c - 如何从main中的函数读取二维数组

标签 c function multidimensional-array

好吧,我确信这很简单,但我不知道如何使用函数。到目前为止,我能够处理 main 中的所有内容,但现在我需要使用函数来完成我所做的几乎所有事情。那么从我下面的代码中,我如何读入(或者正确的术语是)来自 main 的函数?

编辑:为了向大家澄清,我的问题是如何访问我在 main 中返回的数组?

下面的代码接收用户输入指定的不同数量学生的考试成绩。

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

int** getTestData();

int main (){

    ///this is where I'm lost..
    int (*a)[];
    a = getTestData();

}

int** getTestData(){
    int students, numberOfTests, testScores, i, j;
    int** testScoreBank;

    // reads in studens
    scanf("%i", &students);
    testScoreBank = (int**) malloc (sizeof(int)*students);

    for(i=0; i<students;i++){
        //how many number of tests there are
        scanf("%i", &numberOfTests);
        testScoreBank = (int*) malloc (sizeof(int)*numberOfTests);
        for(j=0; j<numberOfTests; j++){
            //the tests themselves
            scanf("%i", &testScores);
            testScoreBank[i][j] = testScores;
        }
    }
    return testScoreBank;
}

最佳答案

好的,这是一个全局变量的例子,如何在函数中填充数组,并在 main() 中访问它

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

int** getTestData();
int numberOfStudents;
int* studentTestSizes;

int main (){
  int** testScoresBank = getTestData();

  int i, j;

  for (i = 0; i < numberOfStudents; i++) {
    for (j = 0; j < studentTestSizes[i]; j++) {
      printf("%d", testScoresBank[i][j]);
    }
  }

  return 0;
}

int** getTestData() {
    int** testScoreBank;

    // reads in studens
    scanf("%d", &numberOfStudents);
    testScoreBank = malloc(sizeof(int*) * numberOfStudents);
    studentTestSizes = malloc(sizeof(int) * numberOfStudents);

    int i;
    for (i = 0; i < numberOfStudents; i++) {
        //how many number of tests there are
        scanf("%d", studentTestSizes + i);
        testScoreBank[i] = malloc(sizeof(int) * studentTestSizes[i]);

        int j;
        for (j = 0; j < studentTestSizes[i]; j++) {
            //the tests themselves
            int testScore;
            scanf("%d", &testScore);
            testScoreBank[i][j] = testScore;
        }
    }
    return testScoreBank;
}

全局变量的替代方法是将全局变量设为局部变量并将指向它们的指针传递给 getTestData 函数,示例如下:

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

int** getTestData();

int main (){

  int numberOfStudents;   // those variables are now here
  int* studentTestSizes;

  int** testScoresBank = getTestData(&numberOfStudents, &studentTestSizes); // passing the pointers so we can change values that are pointed to

  int i, j;
  for (i = 0; i < numberOfStudents; i++) {
    for (j = 0; j < studentTestSizes[i]; j++) {
      printf("%d", testScoresBank[i][j]);
    }
  }

  return 0;
}

int** getTestData(int* numberOfStudentsPtr, int** studentTestSizesPtr) {
    int** testScoreBank;

    // reads in studens
    scanf("%d", numberOfStudentsPtr); // it's already a pointer so we must omit &
    int numberOfStudents = *numberOfStudentsPtr; // will be constant from now on

    testScoreBank = malloc(sizeof(int*) * numberOfStudents);

    *studentTestSizesPtr = malloc(sizeof(int) * numberOfStudents);
    int* studentTestSizes = *studentTestSizesPtr;

    int i;
    for (i = 0; i < numberOfStudents; i++) {
        //how many number of tests there are
        scanf("%d", studentTestSizes + i);
        testScoreBank[i] = malloc(sizeof(int) * studentTestSizes[i]);

        int j;
        for (j = 0; j < studentTestSizes[i]; j++) {
            //the tests themselves
            int testScore;
            scanf("%d", &testScore);
            testScoreBank[i][j] = testScore;
        }
    }
    return testScoreBank;
}

关于c - 如何从main中的函数读取二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32894388/

相关文章:

c - AVX2 1x mm256i 32 位到 2x mm256i 64 位

c - 通过引用传递,不同基类型的错误

javascript - 为什么这现在作为一个返回声明?

javascript - 返回未定义值的简单 Javascript 函数

c# - 在多维数组和单个数组之间存储数据的最有效方法是什么?

c++ - 将多维数组传递给函数

c - 在 C 中初始化 const 变量

c - 如何将 char* 复制到 const char*?

mysql - 如何使用表数据调用存储过程?

java - 格式化二维数组中的 double