- (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 to Character
#include <iostream>
using std::cout;
using std::endl;
int main()
{
char word[] = "Tsai Ing-wen";
char* p = word;
std::cout << word[5] << std::endl;
std::cout << p+5 << std::endl;
std::cout << *(p+5) << std::endl;
return 0;
}
- (10%)
Assume this is a 64-bit computer. 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>
int main(){
const int MAX_LEN = 80;
char msg[MAX_LEN] = "ALICE";
char* p = msg;
char msg2[] = "BOB";
std::cout << sizeof(msg) << '\t' << sizeof(msg2) << std::endl;
std::cout << sizeof(msg[0]) << '\t' << sizeof(p) << std::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).
// Pointer Operation
#include <iostream>
int main(){
int n[] = {2, 0, 1, 8};
int* p = &n[0];
int* p1 = p;
std::cout << *p << (*p)++ << std::endl;
std::cout << *(++p);
std::cout << *(p++) << std::endl;
std::cout << ++(*p) << *(p1++) << *p << std::endl;
return 0;
}