Tell me why can't constant values be used to define an array's initial size
Submitted by: MuhammadThere 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.
Submitted by: Muhammad
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.
Submitted by: Muhammad
Read Online Data Structure Arrays Job Interview Questions And Answers
Top Data Structure Arrays Questions
☺ | What will be output if you will execute following c code? |
☺ | What is Array? |
☺ | What is two-dimensional array? |
☺ | Explain Array of pointers? |
☺ | Tell me why can\'t constant values be used to define an array\'s initial size |
Top Data Structure Categories
☺ | Data Center Manager Interview Questions. |
☺ | Sort And Searching Interview Questions. |
☺ | Creative UI/UX Designers Interview Questions. |
☺ | Data Structure Linked list Interview Questions. |
☺ | Data Structure Arrays Interview Questions. |