Arrays

  Home  Data Structure  Arrays


“Arrays job interview questions and answers guide. The one who provides the best Arrays answers with a perfect presentation is the one who wins the interview race. Learn Data Structure Arrays and get preparation for the job of Arrays”



31 Arrays Questions And Answers

23⟩ Tell me why can't constant values be used to define an array's initial size

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.

 175 views

28⟩ Can you please explain the difference between string and an array?

An array is an array of anything. A string is a specific kind of an array with a well-known convention to determine its length.

There are two kinds of programming languages: those in which a string is just an array of characters, and those in which it's a special type. In C, a string is just an array of characters (type char), with one wrinkle: a C string always ends with a NUL character. The "value" of an array is the same as the address of (or a pointer to) the first element; so, frequently, a C string and a pointer to char are used to mean the same thing.

An array can be any length. If it's passed to a function, there's no way the function can tell how long the array is supposed to be, unless some convention is used. The convention for strings is NUL termination; the last character is an ASCII NUL ('') character.

String literals are arrays of characters (type char), not arrays of constant characters (type const char). The ANSI C committee could have redefined them to be arrays of const char, but millions of lines of code would have screamed in terror and suddenly not compiled. The compiler won't stop you from trying to modify the contents of a string literal. You shouldn't do it, though. A compiler can choose to put string literals in some part of memory that can't be modified-in ROM, or somewhere the memory mapping registers will forbid writes. Even if string literals are someplace where they could be modified, the compiler can make them shared.

 158 views