这是一个初学者的问题,但我只是想对这里的指针感到满意。
如果我创建一个这样的数组数组:
int n; // n lines
cin>>n; // read in first line, number of lines in array of arrays
int **arrays =new int *[n]; // create pointer of pointer of size n
for (int i = 0; i < n; i++) { // looping through n lines to fill each
int sz; // first int of each line is line size
cin>>sz; // read in line size
int *line = new int [sz]; // create line of size sz
for (int j = 0; j < sz; j++) { // loop through individual ints in line
int x; // declare individual int
cin>>x; // read in individual int
line[j] = x; // fill line with each x
}
*(arrays+i)=line; // fill lines of 2D array
}
/*
Sample input:
2 // n lines
3 1 5 4 // first int of each line is line length
5 1 2 8 9 3
*/
在我创建之后int **arrays
有没有办法在每个子数组的长度的事实之后获取信息,比如strlen()
for int*
,以便我可以使用双for
循环打印出来?或者,如果我想打印出来,我是否需要首先跟踪每行的长度?