This is a beginner question, but I'm just trying to get comfortable with pointers to pointers here.
If I create an array of arrays like this:
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
*/
After I've created int **arrays
Is there any way to obtain information after the fact on the length of each subarray, something like a strlen()
for int*
's, so that I can print it out using a double for
loop? Or if I wanted to print it out would I need to keep track of the length of each line in the first place?