-3

I use the const for assigning the variable but the array shows an error "variable-sized object may not be initialized"

#include <stdio.h>
int main()
{

    const int city=10;
    const int week=2;
    int arr[city][week]={34,34,64,23,65,22,65,77,42,74,22,88,46,34,77,53,63,457,234,723};
    for(int i=0;i<city;i++)
    {
        for(int j=0;j<week;j++)
        {
            printf("%d",arr[city][week]);
        }
    }
}
shivitch
  • 1
  • 2
  • Not exactly a dupe, but you should read https://stackoverflow.com/questions/4486442/how-does-const-differ-in-c-and-c and https://stackoverflow.com/questions/4486326/does-const-just-mean-read-only-or-something-more – Gerhardh Nov 08 '20 at 09:47
  • 1
    I assume in your real code you don't have a `==` but a `=` because otherwise the error message doesn't make much sense. – Gerhardh Nov 08 '20 at 09:49

3 Answers3

0

You have several issues.

  1. you've posted it in the subject, is that you can't use const int for the array length and then initialize it, because it will become a variable-sized object, and you can't initialize it in with constant numbers. You can use #ifdef to define city/week, and then it will not happen.

  2. You've used compare (==) instead of assignment (=)

change:

int arr[city][week]=={34,34,64,23,65,22,65,77,42,74,22,88,46,34,77,53,63,457,234,723};

to:

int arr[city][week]={34,34,64,23,65,22,65,77,42,74,22,88,46,34,77,53,63,457,234,723};
  1. you need to change your printf and probably add line break:

    printf("%d\n",arr[i][j]);

so to summarize:

#include <stdio.h>
#define city 10
#define week 2
int main()
{
    int arr[city][week]={34,34,64,23,65,22,65,77,42,74,22,88,46,34,77,53,63,457,234,723};
    for(int i=0;i<city;i++)
    {
        for(int j=0;j<week;j++)
        {
            printf("%d\n", arr[i][j]);
        }
    }
}
Shlomi Agiv
  • 1,183
  • 7
  • 17
  • The error message indicates the `==` is not present in the original source code, as that message is a consequence of attempting to initialize, with `=`, a variable length array. If there were a `==` in the code at that point, the message would have been different. So we know the `==` in the question is a typo made in preparing the question and therefore should be fixed by an edit to the question or a request to the OP, not by posting an answer. In other words, this answer does not answer the true question being asked about. – Eric Postpischil Nov 08 '20 at 11:47
  • @EricPostpischil you're right, my answer solves the posted code – Shlomi Agiv Nov 08 '20 at 11:54
  • It does not “solve” the posted code either, since it leaves it with an initialization of a variable length array, violating the constraint in C 2018 6.7.9 3. – Eric Postpischil Nov 08 '20 at 12:10
  • thanks for clarifying that, changed my answer accordingly – Shlomi Agiv Nov 08 '20 at 13:04
0

You are using const qualified variables for your array dimension and not "integer constant expressions". For C, this means that your array is a variably modified type, for which C does not allow initializers.

As others already told you, if initialization would be allowed, the syntax has to be = and not ==.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
-1

== compares, = assigns
you even would print the same output
i would also prefer to use {a,b},{c,d}...

jak1
  • 3
  • 3
  • 1
    The error message indicates the `==` is not present in the original source code, as that message is a consequence of attempting to initialize, with `=`, a variable length array. If there were a `==` in the code at that point, the message would have been different. So we know the `==` in the question is a typo made in preparing the question and therefore should be fixed by an edit to the question or a request to the OP, not by posting an answer. In other words, this answer does not answer the true question being asked about. – Eric Postpischil Nov 08 '20 at 11:47