A very interesting application
Keywords: iterative problem, one-dimensional array range
For example, there is a structure, it is defined as:
Typedef struct mystuc
{
int NID;
Char * psztext;
} Mystuc;
Then an instance of MyStuc can be:
Mystuc mt [] = {
{0, "Text 1"},
{1, "Text 2"},
{2, "Text 3"}
}
In this case, we can easily convert, and ensure that the result is equivalent:
MySTUC MT [3];
MT [0]. nid = 0;
MT [0] .psztext = "Text 1";
...
MT [2] .psztext = "Text 2";
The previous MT definition is initialized, and the compiler determines the dimension size; the latter one will be initialized before defining, and the dimension size is specified. Obviously, their space is the same. But in iteration, how to apply it? Look at the example below:
INT NMAX = SizeOf (MT) / SizeOf (MT [0]);
For (int i = 0; i { // do something MT [i] .... } It has the advantage that the automatic subscript range of MT is automatically restricted. The subscript is int NMAX = SIZEOF (MT) / sizeof (MT [0]); The benefits of this are: no need to intervene the iteration of the entire array, the boundaries are calculated by the program. And the second example: INT nmax = 3; For (int i = 0; i { // do something MT [i] .... } The subscript is a constant, which is to be specified by the programmer, and the change of the MT range requires an external intervention of the iterative range. The advantage of this method is that the range of iterations can be flexibly specified, which may be part of the object.