Home > Enterprise >  I can't access a global array in c
I can't access a global array in c

Time:03-03

Hello first thing first forgives me if I have mistakes in my English, I am beginner in c and I need help with this problem please

//global variables
int RangeOfArray; 
int arr[RangeOfArray-1]; // error: array bound is not an integer constant before ']' token


void functionOne(){} // I need to access the array here.

void functionTwo(){} // as well here.


int main (){

 cout<<"Type the length number of the array  :  ";
 cin >> RangeOfArray;`

}

As you can see I need the array (arr) everywhere in my program but I can't why? I don't know

CodePudding user response:

An array needs a size that can be known at compile time. RangeOfArray is not known at compile time. Also, you are declaring the array and then trying to assign a size to it, which is not possible. You'll need dynamic arrays for this purpose:

#include <iostream>

int RangeOfArray;
int* arr;

int main() {

    std::cout << "Type the length number of the array  :  ";
    std::cin >> RangeOfArray;

    arr = new int[RangeOfArray];
}

..or preferably, std::vector:

#include <iostream>
#include <vector>

int main() {

    std::cout << "Type the length number of the array  :  ";
    int vec_size; std::cin >> vec_size;

    std::vector<int> vec;
    vec.resize(vec_size);
}

Any of the 2 options work.

CodePudding user response:

In these declarations

//global variables
int RangeOfArray; 
int arr[RangeOfArray-1]; // error: array bound is not an integer constant before ']' token

there is declared the global variable RangeOfArray that is implicitly initialized by zero and then there is declared the variable length array arr with the size -1 that is implicitly converted to the maximum value of the type size_t due to the usual arithmetic conversions because in C an expression that specifies a size of an array in its declaration is converted to the type size_t.

For starters variable length arrays is not a standard C feature. And moreover you may not declare a variable length array with static storage duration.

And secondly using the expression -1 as the size of an array does not make a sense.

If you need a global variable that simulates an array then use the standard container std::vector<int>.

For example

#include <iostream>
#include <vector>

//global variables
std::vector<int> arr;


void functionOne(){ /* ... */ } // I need to access the array here.

void functionTwo(){ /* ... */ } // as well here.


int main()
{
    size_t RangeOfArray;

    std::cout<<"Type the length number of the array  :  ";
    std::cin >> RangeOfArray;`

    arr.resize( RangeOfArray );
    //...
}

The vector provides member function size that reports the current number of elements stored in the vector. So you need not to make the variable RangeOfArray global.

Pay attention to that it is not a good idea to use global variables.

So you could declare the vector in main and pass it by reference to functions where it is required.

CodePudding user response:

why [I cannot use the array]?

The error message that you quoted explains why:

error: array bound is not an integer constant before ']' token

The size of an array must be compile time constant. RangeOfArray-1 is not compile time constant. Hence it cannot be used as the size of an array.

If you want to create an array whose size is determined at runtime, you must use dynamic storage. Simplest solution is to use std::vector from the standard library.


Another issue is that you try to use the variable before it has been assigned a value (although it has been zero-initialised). That approach doesn't work in imperative programming languages such as C . You must assign a value before you can access it.

CodePudding user response:

Mistake 1

In standard C , the size of an array must be a compile time constant. So when you wrote:

int RangeOfArray; 
int arr[RangeOfArray-1]; //not standard c  

The statement int arr[RangeOfArray-1]; is not standard C since RangeOfArray - 1 is not a constant expression.

Mistake 2

From Array declarators documentation:

If the expression is a constant expression, it shall have a value greater than zero.

Now even if RangeOfArray was a constant expression, due to static initialization, RangeOfArray will be initialized with 0 and so the expression RangeOfArray - 1 evaluates to the negative integer -1. And according to the above quoted statement, int[-1]; isn't valid.


Better would be to use std::vector as shown below:

#include <iostream>
#include<vector>
//global variables
 
std::vector<int> arr; // empty vector


void functionOne(){
    std::cout<<"functionOne called with: "<<arr.size()<<std::endl;
} 

void functionTwo(){
    std::cout<<"functionTwo called with: "<<arr.size()<<std::endl;
} 


int main (){
 int RangeOfArray = 0;
 std::cout<<"Type the length number of the array  :  ";
 std::cin >> RangeOfArray;
 
 //resize the vector named arr 
 arr.resize(RangeOfArray);
 
 //call functionOne 
 functionOne();
 
 //call functionTwo
 functionTwo();

}
  • Related