dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Need something explained to me about using a constant with a function and an array

  1. #1
    Join Date
    Sep 2019
    Posts
    11

    Need something explained to me about using a constant with a function and an array

    I'm working with the bubble sort to sort the values in my array into ascending order. There is something I noticed while trying different things to get the code to compile. Initially I wrote my program like this:
    Code:
    void sortArray(int values[], int sizeofArray);
    
    int main()
    {
        int arraySize = 8;
    	int numbers[arraySize] = {7, 58, 3, 42, 81, 6, 23, 37};
    
    	cout << "Array values in original order: ";
    
    	for(int i = 0; i < arraySize; i++)
    		cout << numbers[i] << " ";
    
    	sortArray(numbers, arraySize);
    
    	getch();
    	return 0;
    }
    But the code would not compile. The compiler complained telling me a constant expression was required. Then I placed the keyword const in front of int arraySize like this:
    Code:
    void sortArray(int values[], int sizeofArray);
    
    int main()
    {
    	const int arraySize = 8;
    	int numbers[arraySize] = {7, 58, 3, 42, 81, 6, 23, 37};
    
    	cout << "Array values in original order: ";
    
    	for(int i = 0; i < arraySize; i++)
    		cout << numbers[i] << " ";
    
    	sortArray(numbers, arraySize);
    
    	getch();
    	return 0;
    }
    Now the program compiles fine. I don't quite understand why the keyword const is required.

  2. #2
    Join Date
    Jan 2018
    Posts
    23
    const is required because the c++ compiler needs to know at compile time the size of the array. If const is used, then the compiler does know. If const is not used, then the value is determined at run-time like any other variable. As such, the compiler can't determine the size of the array at compile time and hence reports an error.

  3. #3
    Join Date
    Sep 2019
    Posts
    11
    Quote Originally Posted by 2kaud View Post
    const is required because the c++ compiler needs to know at compile time the size of the array. If const is used, then the compiler does know. If const is not used, then the value is determined at run-time like any other variable. As such, the compiler can't determine the size of the array at compile time and hence reports an error.
    But if I declare a variable called arraySize and give it the value of 8 and then use that variable between the array brackets, why would the compiler not know the size of the array?

  4. #4
    Join Date
    Jan 2018
    Posts
    23
    Any variable not declared as const is set at run-time, not compile time. What if you have:

    Code:
    int arraySize = 7;
    ...
    arraySize += 2;
    int numbers[arraySize] = {7, 58, 3, 42, 81, 6, 23, 37};
    In this case arraySize can only be determined at run-time. But as arrays are allocated on the stack, their size needs to be known at compile time. Hence the requirement that their size is either a number or a const variable (or a constexpr - but you probably haven't come across this yet). When the number of elements are not known at compile time, then there are other c++ containers (eg vector) that can be used. You'll come on to them at some point as you learn more about c++.

Similar Threads

  1. Replies: 0
    Last Post: 01-28-2002, 11:48 AM
  2. Constant string array in C# ?
    By K.C. in forum .NET
    Replies: 3
    Last Post: 01-04-2002, 11:27 AM
  3. Replies: 0
    Last Post: 08-27-2001, 11:33 AM
  4. Re: Death of VB as we knew it. - explained.
    By benjaminkv in forum .NET
    Replies: 9
    Last Post: 10-30-2000, 07:53 PM
  5. Blank responses explained!...
    By John McGuinness in forum C++
    Replies: 0
    Last Post: 07-08-2000, 12:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center