1. The following two are equivalent to each other (but not equivalent to any others):
char stringVar[10] = "Hello";
char stringVar[10] = {'H', 'e', 'l', 'l', 'o', '\0'};
The following two are equivalent to each other (but not equivalent to any others):
char stringVar[6] = "Hello";
char stringVar[] = "Hello";
The following is not equivalent to any of the others:
char stringVar[10] = {'H', 'e', 'l', 'l', 'o'};
2. "DoBeDo to you"
3. The declaration means that stringVar
has room for only six characters (including the null character '\0'
). The function strcat
does not check that there is room to add more characters to stringVar
, so strcat
will write all the characters in the string “and Good-bye.” into memory, even though that requires more memory than has been assigned to stringVar
. This means memory that should not be changed will be changed. The net effect is unpredictable, but bad.
4. If strlen
were not already defined for you, you could use the following definition:
int strlen(const char str[])
//Precondition: str contains a string value terminated
//with '\0'.
//Returns the number of characters in the string str (not
//counting '\0').
{
int index = 0;
while (str[index] != '\0')
index++;
return index;
}
5. The maximum number of characters is five because the sixth position is needed for the null terminator ('\0'
).
1
1
5 (including the '\0'
)
2 (including the '\0'
)
6 (including the '\0'
)
7. These are not equivalent. The first of these places the null character '\0'
in the array after the characters 'a'
, 'b'
, and 'c'
. The second only assigns the successive positions 'a'
, 'b'
, and 'c'
but does not put a '\0'
anywhere.
int index = 0;
while (ourString[index] != '\0' )
{
ourString[index] = 'X';
index++;
}
If the C-string variable does not have a null terminator, '\0'
, the loop can run beyond memory allocated for the C string, destroying the contents of memory there. To protect memory beyond the end of the array, change the while
condition as shown in (b).
while (ourString[index] != '\0' && index < SIZE)
#include <cstring> //needed to get the declaration of strcpy
. . .
strcpy(aString, "Hello");
11. I did it my way!
12. The string “good, I hope.” is too long for aString
. A chunk of memory that doesn’t belong to the array aString
will be overwritten.
13. Enter some input:
The
time is now.
The-timeEND OF OUTPUT
14. The complete dialogue is as follows:
Enter a line of input:
May the hair on your toes grow long and curly.
May t<END OF OUTPUT
15. A*string<END OF OUTPUT
16. A string is a joy forever!<END OF OUTPUT
17. The complete dialogue is
Enter a line of input:
Hello friend!
Equal
Remember, cin
stops reading when it reaches a whitespace character such as a blank.
18. Hello Jello
19. The program is legal. The output is
0 1 2 3 4 5 6 7 8 9
Note that changing v does not change copy. A true independent copy is made with the assignment
copy = v;
20. The size is the number of elements in a vector, whereas the capacity is the number of elements for which there is memory allocated. Typically, the capacity is larger than the size.