Quiz 004: Mysterious array initialization

Description
Fill in the blanks to make the program output the specified result

#include <iostream>
using namespace std;

int main()
{
     
     
	int * a[] = {
     
     
// 在此处补充你的代码
};
	
	*a[2] = 123;
	a[3][5] = 456;
	if(! a[0] ) {
     
     
		cout << * a[2] << "," << a[3][5];
	}
	return 0;
}

Input
None
Output
123,456
Sample input
None
Sample output
123,456

Analysis: First, it int* a[]is an array of pointers ([] priority is higher than *), that is, array elements are pointers to int. int(*a) []It is an array pointer.
Then observe that when *a[0] is 0 or nullptr, print, so the first element should be set to'\0', and the second element should be the same (because it is useless...).
Also *a[2] =123, so the third element must be an array with at least 1 element in size. At first I thought of {1}, but it did not mean an array , and then racked my brains and thought of using the new method, new int[1]that's it new int [6]. It can be run at this time.
ans:
int * a[] = {'\0','\0',new int[1],new int[6]};

Guess you like

Origin blog.csdn.net/ZmJ6666/article/details/108550634