正在加载图片...
-11- Problem 4:Useful Pointers(20 points) If we statically allocate an array,with a declaration like inta[4][10]; then all of the rows naturally have the same number of elements.We will consider the first index to be the number of rows,so this array has 4 rows of 10 ints. When we studied dynamic allocation,we developed a way to dynamically allocate a two- dimensional array,using a structure that looked like this: When we did this,we again made all the rows the same length,but that is not a requirement.If we want to,we could have a "jagged"array: The only difficulty with this sort of structure is that we would need a way to know how many elements are on each row.The simple solution is to keep another dynamically allocated array that holds the row widths.We could use a definition like this: typedef struct int *sizes; int **rows; int nRows; } jArrayT; sizes will be an array the holds the row lengths,rows is the array of row pointers,and nRows tell how many rows there are.After declaring ajrrayr,our first step would be to initialize it.That would involve setting the sizes to zero and the row pointers to NuLL.Given the following declaration for a pointer to a jArrayT: jArrayT *myArray;– 11 – Problem 4: Useful Pointers (20 points) If we statically allocate an array, with a declaration like int a[4][10]; then all of the rows naturally have the same number of elements. We will consider the first index to be the number of rows, so this array has 4 rows of 10 ints. When we studied dynamic allocation, we developed a way to dynamically allocate a two￾dimensional array, using a structure that looked like this: When we did this, we again made all the rows the same length, but that is not a requirement. If we want to, we could have a "jagged" array: The only difficulty with this sort of structure is that we would need a way to know how many elements are on each row. The simple solution is to keep another dynamically allocated array that holds the row widths. We could use a definition like this: typedef struct { int *sizes; int **rows; int nRows; } jArrayT; sizes will be an array the holds the row lengths, rows is the array of row pointers, and nRows tell how many rows there are. After declaring a jArrayT, our first step would be to initialize it. That would involve setting the sizes to zero and the row pointers to NULL. Given the following declaration for a pointer to a jArrayT: jArrayT *myArray;
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有