I'm a bit new in C and I'm trying to write some simple code that gets some grades for a few students and store them in a two-dimensional array. The problem is it won't run because the array is not declared. Why do I need to declare something before I can use it and how can I simply declare it without running through the whole loop and declaring some value?
我在C中有点新,我正在尝试编写一些简单的代码,为一些学生获得一些成绩并将它们存储在二维数组中。问题是它不会运行,因为没有声明数组。为什么我需要在使用之前声明一些东西,如何在不运行整个循环并声明某些值的情况下简单地声明它?
This is my code for now:
这是我现在的代码:
#include <stdio.h>
int main()
{
int students = 2, courses = 2;
int grades[students][courses];
for(int i=0; i<students; i++)
{
for(int j=0; j<courses; j++)
{
printf("Student %d, course %d grade: ", i, j);
scanf("%d", &grades[i][j]);
}
}
}
4 个解决方案
#1
4
Why do I need to declare something before I can use it
为什么我需要在使用之前声明一些东西
Because that's how C language is defined. You have to declare things before you can use them, period. You have to tell the compiler what "it" is before you can use "it". Sometimes C can be rather permissive in this regard, willing to get by with only a "partial" declaration. But in any case at least a partial declaration of some sort is required.
因为这就是C语言的定义方式。您必须在使用它们之前声明事物,期间。在使用“它”之前,你必须告诉编译器“它”是什么。在这方面,有时候C可以相当宽容,愿意只通过“部分”声明。但无论如何至少需要某种部分声明。
Your (commented-out) declaration is correct
您的(已注释掉的)声明是正确的
int grades[students][courses];
Note though that in order to use non-constants students
and courses
as array sizes you need a modern C compiler (C99 or later), which supports Variable Length Arrays (VLA). "Classic" C (C89/90) does not allow non-constant array sizes.
请注意,为了将非常量学生和课程用作数组大小,您需要一个现代C编译器(C99或更高版本),它支持可变长度数组(VLA)。 “Classic”C(C89 / 90)不允许非常数阵列大小。
Then you have to assign your array elements some meaningful values. This has to be done using a cycle.
然后,您必须为数组元素分配一些有意义的值。这必须使用循环来完成。
#2
2
I'm not sure what you're asking with your second question, but it looks like you have the declaration for your 2-D array right. int grades[students][courses];
should give you a 2-D array of fixed size of integers. You need to declare values to tell the compiler what you're actually referring to.
我不确定你的第二个问题是什么问题,但看起来你的二维阵列声明正确。 int grade [学生] [课程];应该给你一个固定大小的整数二维数组。您需要声明值以告诉编译器您实际指的是什么。
#3
1
The declaration lets the computer reserve storage space in which to put the values you want to save in the array. If you don't have a declaration, there's no place reserved to save those values.
该声明允许计算机保留存储空间,以便将要保存的值放在数组中。如果您没有声明,则没有保留这些值的地方。
It's like saying "I want to write something down, why do I have to get a piece of paper first?"
这就像是说“我想写下一些东西,为什么我要先拿一张纸呢?”
#4
0
To declare the Array you would put
声明要放置的数组
int grades[2][2];
as the first line in your main. You could set the size to something larger if you wanted to include more students or grades.
作为你主要的第一行。如果您想要包含更多学生或成绩,您可以将大小设置为更大的大小。
#1
4
Why do I need to declare something before I can use it
为什么我需要在使用之前声明一些东西
Because that's how C language is defined. You have to declare things before you can use them, period. You have to tell the compiler what "it" is before you can use "it". Sometimes C can be rather permissive in this regard, willing to get by with only a "partial" declaration. But in any case at least a partial declaration of some sort is required.
因为这就是C语言的定义方式。您必须在使用它们之前声明事物,期间。在使用“它”之前,你必须告诉编译器“它”是什么。在这方面,有时候C可以相当宽容,愿意只通过“部分”声明。但无论如何至少需要某种部分声明。
Your (commented-out) declaration is correct
您的(已注释掉的)声明是正确的
int grades[students][courses];
Note though that in order to use non-constants students
and courses
as array sizes you need a modern C compiler (C99 or later), which supports Variable Length Arrays (VLA). "Classic" C (C89/90) does not allow non-constant array sizes.
请注意,为了将非常量学生和课程用作数组大小,您需要一个现代C编译器(C99或更高版本),它支持可变长度数组(VLA)。 “Classic”C(C89 / 90)不允许非常数阵列大小。
Then you have to assign your array elements some meaningful values. This has to be done using a cycle.
然后,您必须为数组元素分配一些有意义的值。这必须使用循环来完成。
#2
2
I'm not sure what you're asking with your second question, but it looks like you have the declaration for your 2-D array right. int grades[students][courses];
should give you a 2-D array of fixed size of integers. You need to declare values to tell the compiler what you're actually referring to.
我不确定你的第二个问题是什么问题,但看起来你的二维阵列声明正确。 int grade [学生] [课程];应该给你一个固定大小的整数二维数组。您需要声明值以告诉编译器您实际指的是什么。
#3
1
The declaration lets the computer reserve storage space in which to put the values you want to save in the array. If you don't have a declaration, there's no place reserved to save those values.
该声明允许计算机保留存储空间,以便将要保存的值放在数组中。如果您没有声明,则没有保留这些值的地方。
It's like saying "I want to write something down, why do I have to get a piece of paper first?"
这就像是说“我想写下一些东西,为什么我要先拿一张纸呢?”
#4
0
To declare the Array you would put
声明要放置的数组
int grades[2][2];
as the first line in your main. You could set the size to something larger if you wanted to include more students or grades.
作为你主要的第一行。如果您想要包含更多学生或成绩,您可以将大小设置为更大的大小。