1

I was trying topological sorting but I got error with the declaration of array.I had attached the part in which i got error. If I replace the n in visited array with the value 6 then the algorithm work fine . I do not understand why this is happening? Here is the Code in which I was getting error: Code On Ideone

#include<iostream>
#include<vector>
using namespace std;

int n=6;
int visited[n]={0};
int main()
{
    cout<<visited[0];
}
randomUser
  • 653
  • 6
  • 23

3 Answers3

3

There are two things, in your code

 int visited[n]={0};

However, following the definition of constant-expression in C++, you can do something like

 const int n=6;
 int visited[n]={0};  //not a VLA anymore
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Your array's memory (e.g. where visited is stored in memory and its size) is decided in compile time. Whereas 'n' is not known at compile time since its a variable and not a constant (Even if you place a value in it). C++ does not allow this kind of declaration. – Lior Apr 09 '20 at 05:57
  • Your example does not initialize the array `visited` properly, but only the first element. – salchint Apr 09 '20 at 06:50
  • @salchint How is that? – Sourav Ghosh Apr 09 '20 at 10:31
  • @SouravGhosh This code does only set the first array element to `0`. The rest is untouched/uninitialized, hence set to random values. `int visited[]={0, 0, 0, 0, 0, 0};` can be used to initialize all the arrays elements (in fact here the number of `0` defines the array's length). But sure, this is not the only way to initialize an C-like array. – salchint Apr 09 '20 at 23:17
  • 1
    @salchint you are wrong. The code is correct and it initialize the first with 0 and the rest with 0. – Cătălina Sîrbu Aug 10 '21 at 14:06
1

While the other answers are correct in telling you what the problem is, they miss out on giving you an actual way to declare dynamic arrays.
From the topic being "topological sorting", I assume that you only were testing when you declared your variable n and that it might be an actual variable (rather than a constant) later.

If so, please have a look at How to create a dynamic array of integers.

In short: You can create a pointer that points to the start of an array on the heap:

int* visited = new int[n];

which works just like you are used to with arrays, but must be freed with delete in order to avoid leaking memory.

Better is using a container class that does this for you:

std::vector<int> visited(n);

Put any line in your code and it will work as intended.
As a general recommendation, you should read tutorials on how to work with the STL, including getting an overview over its container classes.

Aziuth
  • 3,652
  • 3
  • 18
  • 36
0

Defining an array this way means you want its memory to have static duration, because it is a global variable. In addition you intend to use a C-like array. The type-system of C++ works in a way that visited is, in the working situation, of type int[6]!! I.e. the number of elements is part of the type. Hence this information needs to be first, available at compile time and second, be immutable. n of your example can change at any time and is therefore not qualified to be used in the array type.

You can do the following instead:

constexpr unsigned int n {6};
int visited[n]={0};

... or if you also want to initialize the array in a proper way, use this for example:

int visited[]={0, 0, 0, 0, 0, 0};
salchint
  • 371
  • 3
  • 10