C++ Primer - Chapter 2 (Variables & Basic Types) Flashcards
True or false? When we define a const with the same name in multiple files, it is as if we had written definitions for separate variables in each file.
True.
What is a “reference”?
A compound type that “refers to” another type.
A reference defines an alternative name for an object.
Name the primitive data types in C++.
The arithmetic types (i.e. boolean value, characters, integers, and floating-point numbers) and the special type, void.
What is wrong with the following code?
int i=0;
int *p=i;
int i=0; int *p=i; // Error; an integral value cannot be assigned to a pointer.
Is this code legal? If not, why not? const double pi = 3.14; double *ptr = pi; const double *cptr = π *cptr = 42;
The code is not legal.
const double pi = 3.14; double *ptr = pi; // Error, as ptr is a plain pointer. const double *cptr = π // Okay, as cptr can point to a double that is const. *cptr = 42; // Error, as we cannot assign to *cptr.
Are arithmetic expressions involving a signed and unsigned integer typically cast to a signed or unsigned integer?
Further, what is the result of i+u in the following?
int i=-42;
unsigned u=10;
Unsigned.
The sum i+u yields (2^32)-32. Note, that i is converted to an unsigned before the expression is evaluated.
A ________ is a name that is a synonym for another type.
A type alias is a name that is a synonym for another type.
Explain each of the following definitions. Indicate whether any are illegal and, if so, why. int i = 0; (a) double* dp = &i; (b) int *ip = i; (c) int *p = &i;
int i = 0; (a) double* dp = &i; // Invalid, the types do not match. (b) int *ip = i; // Invalid, cannot assign an integral value to a pointer.. (c) int *p = &i; // Valid, the types match.
What form does the declarator of a reference take?
We define a reference type by writing a declarator of the form &d, where d is the name being declared.
Which of the following initializations are legal? Explain why. int i=0; (a) const int *p1 = &i; (b) const int &const r2; (c) const int i2 = i, &r = i;
int i=0; (a) const int *p1 = &i; // Legal, but p1 can't be used to change the value of i. (b) const int &const r2; // Illegal; a reference is not an object, so it can't be const, and it must be initialised. (c) const int i2 = i, &r = i; // Legal if i is an int or const int.
Can you define a reference to a reference?
No, references are not objects so you cannot create a reference to a reference.
What is the result of:
int *ip1, *ip2;
double dp, *dp2;
Note that the * must be repeated for each pointer variable:
int *ip1, *ip2;
Both ip1 and ip2 are pointers to int.
double dp, *dp2;
Here, dp2 is a pointer to double and dp is a double.
Which, if any, of the following assignments are invalid? If they are valid, explain what they do. int i = 0, &r1 = i; double d = 0, &r2 = d; (a) r2 = 3.14159; (b) r2 = r1; (c) i = r2; (d) r1 = d;
int i = 0, &r1 = i; double d = 0, &r2 = d; (a) r2 = 3.14159; // Valid, and also changes the value of d as r2 is bound to d. (b) r2 = r1; // Valid, the value of r2 and d are changed to 0. (c) i = r2; // Valid. (d) r1 = d; // Valid, i and r1 will have value 0.
What is a nonprintable character? Give an example.
A character with no visible representation, such as a control character, a backspace, newline, and so on.
What can constexpr references or pointers be initialised with?
A constexpr pointer can be initialised with the nullptr literal or literal 0.
A constexpr pointer or reference can also point to, or bind to, an object that remains at a fixed address.
True or false? (a) and (b) below are equivalent.
typedef char *pstring;
(a) const pstring cstr = 0;
(b) const char *cstr = 0;
False.
typedef char *pstring; (a) const pstring cstr = 0; (b) const char *cstr = 0; // In (a), because of the typedef, when pstring is used in a declaration, the base type is a pointer to char. // In (b) the base type is a char and * is part of the declarator.
What is a “compound type”? Give two examples of a compound type.
A type that is defined in terms of another type. Examples include references and pointers.
How can the type of a integer literal be specified? Is this prefixed or suffixed?
u or U (unsigned)
l or L (long)
ll or LL (long long)
The specification is applied as a suffix.
N.B.: Use capital letters for best practice, e.g. ‘l’ can be mistaken for a ‘1’.
What is a type alias?
A synonym for another type.
Is the following program legal? If so, what values are printed?
int i = 100, sum = 0;
for (int i = 0; i != 10; ++i)
{
sum += i;
}
std::cout «_space;i «_space;” “ «_space;sum «_space;std::endl;
The program:
int i = 100, sum = 0; for (int i = 0; i != 10; ++i) { sum += i; } std::cout << i << " " << sum << std::endl;
is legal, and the output is: 100 45.
Name the problems, if any, of the following code: double dval; double *pd = &dval; double *pd2 = pd; int *pi = pd; pi = &dval;
Note that the types of the pointer and the object to which it points must match:
double dval; double *pd = &dval; // This is okay as the initializer is the address of a double. double *pd2 = pd; // This is okay as the initializer is a pointer to double. int *pi = pd; // This is an error as the types of pi and pd differ. pi = &dval; // This is an error; cannot assign the address of a double to a pointer to int.
What do uninitialised pointers typically use (under most compilers) as an address?
Under most compilers, the bits in memory where the pointer resides are used as an address.
When is decltype((variable)) a reference type?
decltype((variable)) is always a reference type.
What is wrong with the following code?
const int a;
const variables must have explicit initialisers.