- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// sizeof operator
#include <iostream>
using std::cout;
using std::endl;
int main()
{
char* pstr[] = { "George Washington",
"John Adams",
"Thomas Jefferson",
"James Madison",
"James Monroe",
"John Quincy Adams"
};
cout << sizeof pstr[5] << endl;
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// size of a char array
#include <iostream>
using std::cout;
using std::endl;
int main()
{
char str[][18] = { "George Washington",
"John Adams",
"Thomas Jefferson",
"James Madison",
"James Monroe",
"John Quincy Adams"
};
cout << sizeof str[5] << endl;
// What is the size of str[2]?
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Pointer arithmetic
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int data[7] = { 1, 3, 5, 7, 2, 4, 6 };
int* pnum = &data[2];
cout << *(pnum + 2) - *(pnum +1) << endl;
return 0;
}