1. array size
I believe that in C #/Java, more people are willing to use list <t> to replace arrays. On the one hand, list provides more methods, on the other hand, we do not need to specify the size of the array.
In C, since we need to specify the size of the array, in general, we are unable to determine many array things and will change frequently, the best way is to use a macro definition to limit the size of an array.
# DefineSize 10
IntMain (Void)
{
IntA [size];
}
If multiple arrays are contained, it is difficult to remember using macros, so we can use the sizeof operator.
Int Main ( Void )
{
Int A [] = };
Int I;
Printf ("% D" ,( Int ) Sizeof ()/( Int ) Sizeof (A [0]);
For (I = 0; I <( Int ) Sizeof ()/( Int ) Sizeof (A [0]); I ++)
{
A [I] = 0;
}
For (I = 0; I <(Int ) Sizeof ()/( Int ) Sizeof (A [0]); I ++)
{
Printf ( "% D \ n" , A [I]);
}
}
Note: As we have said before, the value returned by sizeof is size_t. Therefore, we recommend that you forcibly convert the type to a controllable type during calculation.
2. array Initialization
In general, when we Initialize an array, We initialize the integer array to 0. What should we do in general?
# DefineSize 5
IntMain (Void)
{
IntA [size] = {0, 0, 0, 0 };
}
If the size is 100, many people will do this.
# DefineSize 100
IntMain (Void)
{
IntA [size];
IntI;
For(I = 0; I <size; I ++)
{
A [I] = 0;
}
}
In fact, we don't have to worry about it.CodeYou can do it.
# DefineSize 100
IntMain (Void)
{
IntA [size] = {0 };
}
In c99, an initialization type is provided so that we can write it like this.
# DefineSize 100
IntMain (Void)
{
IntA [size] = {[5] = 100, [50] = 49 };
}
All other numbers are 0 by default. Let's consider the following code:
# DefineSize 10
IntMain (Void)
{
IntA [size] = {1, 2, 4, 5, [0] = 6, 7, 8 };
}
So what is the result of this Code in c99? This requires us to understand the principle of array initialization.
In fact, the compiler records the location of the next element to be initialized when initializing the array list. For example, when initializing an element with Index = 0, it records 1, and so on, however, when the index is initialized to 5, Index = 1 is first recorded for the next element to be initialized according to its initialization type, and then 6 is initialized for the element with Index = 0. That is to say: the final result should be {6, 7, 8, 4, 5, 0, 0, 0 }.
3. Constant Array
When the array is added with const, it becomes a constant array. The constant array has two main advantages.
1. Tell the user that the array should not be changed.
2. Helps the compiler to discover errors.
4. c99 Variable Length Array
This is a good thing. We don't have to worry about setting the size of the array any more. A large size will cause a waste of space.
In c99, its length will beProgramComputing is performed during execution.
The method is as follows:
IntMain (Void)
{
IntSize;
IntA [size];
Scanf ("% D", & Size );
}
5. Copying Arrays
In many cases, we need to copy the elements of an array to another array. Most of us first think of circular replication.
# DefineSize 10
IntMain (Void)
{
IntA [size];
IntB [size];
IntI;
For(I = 0; I <size; I ++)
{
A [I] = I;
}
For(I = 0; I <size; I ++)
{
B [I] = A [I];
}
For(I = 0; I <size; I ++)
{
Printf ("% D", B [I]);
}
}
In fact, there is also a better way to use the memcpy method, which is an underlying function that copies the memory bytes from one place to another, which is more efficient.
# Include<Stdio. h>
# Include<Stdlib. h>
# Include<String. h>
# DefineSize 10
IntMain (Void)
{
IntA [size];
IntB [size];
IntI;
For(I = 0; I <size; I ++)
{
A [I] = I;
}
Memcpy (B,,Sizeof());
For(I = 0; I <size; I ++)
{
Printf ("% D", B [I]);
}
}