好吧,我确信这很简单,但我不知道如何使用函数。到目前为止,我能够处理 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/