Is it possible to set an array as follows? C ++ language

int arr[*N]={*A}; 

test.cpp: 7: 16: error: variable-sized object 'arr' may not be initialized N and A - the entered variables

  • int arr [* N] = {* A}; - Eugene Kokoshko
  • edit the question by adding all the necessary information. - Grundy Nov.
  • And what's stopping you to check it yourself? - ArchDemon Nov.
  • one
    what language is used? - Grundy Nov.
  • 2
    This is already known DURING IMPLEMENTATION, not compilation. - Harry

2 answers 2

Since there are no addresses at compile time, such a construction is impossible — the size of the array must be known at compile time.

And by the way

  int *A,*N,*D; cin >>*A>>*N>>*D; 

erroneous code. You have uninitialized pointer variables pointing anywhere ... and trying to read data anywhere. Undefined behavior with all the consequences. With luck - the program will end abnormally, with less luck - it will continue to work ...

    The compiler tells you that you are trying to create an array of variable length, when the dimension of the array is determined during program execution. However, the C ++ standard does not allow the creation of such arrays, although some compilers have their own language extensions that allow this.

    Therefore, it will be easier for you if you use the standard class std::vector instead of an array.

    For example,

     std::vector<int> v = { *A }; 

    In the general case, there is no need to initially set the number of elements in the vector, since their number may change as you add or remove elements from the vector.

    In C, you can actually create arrays of variable length, although this support is not necessary for C compilers.