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.
- #define SIZE 10
-
- int main (void)
- {
- int a[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[]={1,3,4,55,6,7,89,9,0};
- int i ;
- printf("%d",(int)sizeof(a)/(int)sizeof(a[0]));
- for(i=0;i<(int)sizeof(a)/(int)sizeof(a[0]);i++)
- {
- a[i]=0;
- }
- for(i=0;i<(int)sizeof(a)/(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?
- #define SIZE 5
-
- int main (void)
- {
- int a[SIZE]={0,0,0,0,0};
- }
If the SIZE is 100, many people will do this.
- #define SIZE 100
-
- int main (void)
- {
- int a[SIZE];
- int i ;
- for(i=0;i<SIZE;i++)
- {
- a[i]=0;
- }
- }
In fact, we don't have to worry about it at all, so we can do it with such a piece of code.
- #define SIZE 100
-
- int main (void)
- {
- int a[SIZE]={0};
- }
In C99, an initialization type is provided so that we can write it like this.
- #define SIZE 100
-
- int main (void)
- {
- int a[SIZE]={[5]=100,[50]=49};
- }
All other numbers are 0 by default. Let's consider the following code:
- #define SIZE 10
-
- int main (void)
- {
- int a[SIZE]={1,2,3,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, the length is calculated during program execution.
The method is as follows:
- int main (void)
- {
- int size;
- int a[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.
- #define SIZE 10
-
- int main (void)
- {
- int a[SIZE];
- int b[SIZE];
- int i ;
- 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>
-
- #define SIZE 10
-
- int main (void)
- {
- int a[SIZE];
- int b[SIZE];
- int i ;
- for(i=0;i<SIZE;i++)
- {
- a[i]=i;
- }
- memcpy(b,a,sizeof(a));
- for(i=0;i<SIZE;i++)
- {
- printf("%d",b[i]);
- }
- }
This article is from the "kym" blog, please be sure to keep this source http://kymsha.blog.51cto.com/647951/290505