0

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?

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]);
        }
    }
}
2
  • 1
    So you wish to make up the language as you go on your merry way. Interesting thought Commented Jul 1, 2015 at 18:25
  • Question is really unclear. If you uncomment that one line everything will work fine. You need to declare grades first because that's how C works. The for-loops are there to get user input on what the values should be, if you want to avoid that you can just hard-code some values. Commented Jul 1, 2015 at 18:28

4 Answers 4

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.

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.

Then you have to assign your array elements some meaningful values. This has to be done using a cycle.

Sign up to request clarification or add additional context in comments.

7 Comments

So can I declare it without looping on the array and declaring each element?
@Omer Aviv: Well, according to your code, the element values have to be read from standard input. If so, there's no way around these cycles. You can use a file as input, thus making this program to read these values from that file (instead of making the user to type them in), but the cycles are necessary. And you are not "declaring each element". Elements are already declared by array declaration int grades[students][courses];. The cycles are there to give each element a meaningful value.
I mean, it pops out these errors before it can even start receiving inputs: expected constant expression, cannot allocate an array of constant size 0, 'grades': missing subscript and 'grades: unknown size. Do I have to make a cycle to define each element with some default value like 0?
@Omer Aviv: In order to use non-constant values as array sizes you need a C99 compiler (or later). Your compiler is apparently not C99, which is why you get that error. In "classic" C (C89/90) you'd have to do #define students 2, i.e. use constant sizes.
@Omer Aviv: VS2013 supports a lot of C99, but it does not support non-constant size arrays (Variable Length Arrays), which is why you get the error. Can you just change your sizes to constants? #define students 2 and #define courses 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.

Comments

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?"

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.