There are times when constant values can be used and there are times when they can't. A C program can use what C considers to be constant expressions, but not everything C++ would accept.When defining the size of an array, you need to use a constant expression. A constant expression will always have the same value, no matter what happens at runtime, and it's easy for the compiler to figure out what that value is. It might be a simple numeric literal:char a[ 512 ];Or it might be a "manifest constant" defined by the preprocessor:#define MAX 512/* ... */char a[ MAX ];Or it might be a sizeof:char a[ sizeof( struct cacheObject ) ];Or it might be an expression built up of constant expressions:char buf[ sizeof( struct cacheObject ) * MAX ];Enumerations are allowed too.An initialized const int variable is not a constant expression in C:int max = 512; /* not a constant expression in C */char buffer[ max ]; /* not valid C */Using const ints as array sizes is perfectly legal in C++; it's even recommended. That puts a burden on C++ compilers (to keep track of the values of const int variables) that C compilers don't need to worry about. On the other hand, it frees C++ programs from using the C preprocessor quite so much.
C++
Topic: Array
Why can't constant values be used to define an array's initial size?
Browse random answers:
Explain about Declaring Arrays ?
Explain about Initializing Arrays?
Explain about Accessing Array Elements ?
What is the difference between an ARRAY and a LIST?
Define pointer and array. Explain the difference between them.
When should we use container classes instead of arrays?
What is Dynamic memory management for array?
What is the difference between class and objects?
What is static class member?
What are methods and fields?
What is an array?
What is a character array?
How do you decide which integer type to use?
Can you store multiple data types in System.Array?
Difference between "vector" and "array"?
How can double dimensional arrays be dynamically initialized in C++?
What is the difference between the System.Array.CopyTo() and System.Array.Clone()?
Why can't constant values be used to define an array's initial size?
What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array?
Delete duplicate elements from array
Is it valid to address one element beyond the end of an array?
Can the sizeof operator be used to tell the size of an array passed to a function?
What is meant by ?method-wars??
Write a program to delete an element from an array?