Chapter 6 "alternative" Array

Source: Internet
Author: User

Dynamic Arrays and string constants are two alternative arrays.

VLA Variable Length array is not supported by c89, and c99 starts to support VLA. But what if I want to use VLA in a compiling environment that only supports c89? We can use dynamic arrays to "simulate" dynamic arrays, which are common in matrix operations and are often used to pass a variable-size matrix to functions. The principle of dynamic array is to use one or more dynamically allocated memory to store the first address of each dimension. In this way, you can access the array data in the form of P [I] [J. However, a dynamic array is not a real array, but a simulation of the array. Because an array name of the array type is a system action, it cannot be done at the user level. Therefore, it can only store the first address, sizeof (P) and sizeof (P [I]) in the form of a pointer. the result is 4 bytes. Although dynamic arrays are created by dynamically allocating memory, the dynamic meaning does not come from here, but is variable size. I think it is very suitable to use the name of "dynamic array", which can be distinguished from VLA variable-length array without losing its size.

The following is an example of creating a dynamic array:

# Include <stdio. h>
# Include <stdlib. h>

Void computedata (int *, Int, INT );

Int main (void)
{
Int idata [100], X, Y;
Do
{
Printf ("the product obtained by multiplying X and Y must be less than 100! ");
Printf ("x = ");
Scanf ("% d", & X );
Printf ("Y = ");
Scanf ("% d", & Y );
}
While (x * Y> 100 );
Computedata (idata, x, y );
Return 0;
}

Void computedata (int * ipsource, int irow, int icolumn)
{
Int ** iptemp, I, j;
Iptemp = (INT **) malloc (irow * sizeof (int *));
For (I = 0; I <irow; ++ I) iptemp [I] = ipsource + I * icolumn;
For (I = 0; I <irow; ++ I) for (j = 0; j <icolumn; ++ J) iptemp [I] [J] + = 1;
Free (iptemp );
Return;
}

The above example adds 1 to all the elements of the dynamic array iptemp. Because it is only an example, the code for checking data validity is omitted. Irow is the upper bound of the first dimension, icolumn is the upper bound of the second dimension, idata is the source data buffer, and the irow * icolumn product cannot exceed the size of the idata buffer, otherwise it will be out of bounds, but it can be smaller than it. In this example, idata is defined as a one-dimensional array. You can also replace it with other types of buffers, such as a dynamically allocated memory or multi-dimensional array, for example, for a 3D array int idata [10] [10] [10], when calling the computedata function, the real parameter idata must be converted:

Computedata (int *) idata, x, y); or computedata (& idata [0] [0] [0], x, y.

The ipsource pointer is used to pass the first address of the buffer. This pointer is used to calculate the addresses of each dimension, so it is better to define it as a first-level pointer, which is more convenient. Iptemp is a second-level pointer, because the memory it points to stores pointers. These pointers point to the first address of each dimension. For iptemp elements, iptemp is second-level. Remember free (iptemp );

The above is an example of defining a two-dimensional dynamic array. The method for creating a multi-dimensional dynamic array is similar to this. The code for creating a three-dimensional dynamic array is as follows:

Void computedata (int * ipsource, int ihigh, int irow, int icolumn)
{
Int *** iptemp, I, J, K;
Iptemp = (INT ***) malloc (ihigh * sizeof (INT **));
For (I = 0; I <ihigh; ++ I) iptemp [I] = (INT **) malloc (irow * sizeof (int *));
For (I = 0; I <ihigh; ++ I) for (j = 0; j <irow; ++ J) iptemp [I] [J] = ipsource + I * irow * icolumn + J * icolumn;
For (I = 0; I <ihigh; ++ I) for (j = 0; j <irow; ++ J) for (k = 0; k <icolumn; ++ K) iptemp [I] [J] [k] + = 1;
For (I = 0; I <ihigh; ++ I) Free (iptemp [I]);
Free (iptemp );
Return;
}

The following describes string constants.
As we all know, the C language does not have a string variable. Therefore, c89 stipulates that a String constant is a character array. Therefore, although the external representation of a String constant is completely different from that of an array, it is indeed a real array. In fact, a String constant itself is the first address of this array and has an array type, perform the sizeof operation on a string constant, for example, sizeof ("abcdefghi"). The result is 10 instead of 4. The main difference between a String constant and a general array is that a String constant is stored in a static storage area, while a general array (non-static) is statically allocated in the stack. Because a String constant is the first address of an array, it can be used as an array reference. For example:

Printf ("% s", & "abcdefghi" [4]);

This prints the efghi string. You can also do this:

Printf ("% s", "abcdefghi" + 4 );

Print the efghi string. In fact, & "abcdefghi" [4] is equivalent to & * ("abcdefghi" + 4). After & * is removed, it is "abcdefghi" + 4.

We can use the character string constants to write interesting programs, such:

# Include <stdio. h>

Int iline = 1;

Int main (void)
{
Printf ("% * s/n", 7-(iline> 4? ILine-4: 4-iline), "*******" + 2 * (iline> 4? ILine-4: 4-iline ));
If (++ iline! = 8) Main ();
Return 0;
}

This program does not use any array reference, and does not use loops, you can print the diamond formed by the * sign. Of course, I do not encourage you to write such code, but it is still very important to use such examples to deepen your understanding of string constants.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.