-
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.
-
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.
-
 Originally Posted by 2kaud
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?
-
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
-
Replies: 0
Last Post: 01-28-2002, 11:48 AM
-
Replies: 3
Last Post: 01-04-2002, 11:27 AM
-
Replies: 0
Last Post: 08-27-2001, 11:33 AM
-
By benjaminkv in forum .NET
Replies: 9
Last Post: 10-30-2000, 07:53 PM
-
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
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|