C Language Study Notes (4)

Source: Internet
Author: User

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.

 
 
  1. #define SIZE 10  
  2.  
  3. int main (void)  
  4. {  
  5.     int a[SIZE];  

 

If multiple arrays are contained, it is difficult to remember using macros, so we can use the sizeof operator.

 
 
  1. int main (void)  
  2. {  
  3.     int a[]={1,3,4,55,6,7,89,9,0};  
  4.     int i ;  
  5.     printf("%d",(int)sizeof(a)/(int)sizeof(a[0]));  
  6.     for(i=0;i<(int)sizeof(a)/(int)sizeof(a[0]);i++)  
  7.     {  
  8.         a[i]=0;  
  9.     }  
  10.     for(i=0;i<(int)sizeof(a)/(int)sizeof(a[0]);i++)  
  11.     {  
  12.         printf("%d\n",a[i]);  
  13.     }  

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?

 
 
  1. #define SIZE 5  
  2.  
  3. int main (void)  
  4. {  
  5.     int a[SIZE]={0,0,0,0,0};  

 

If the SIZE is 100, many people will do this.

 
 
  1. #define SIZE 100  
  2.  
  3. int main (void)  
  4. {  
  5.     int a[SIZE];  
  6.     int i ;  
  7.     for(i=0;i<SIZE;i++)  
  8.     {  
  9.         a[i]=0;  
  10.     }  

 

In fact, we don't have to worry about it at all, so we can do it with such a piece of code.

 
 
  1. #define SIZE 100  
  2.  
  3. int main (void)  
  4. {  
  5.     int a[SIZE]={0};  

 

In C99, an initialization type is provided so that we can write it like this.

 
 
  1. #define SIZE 100  
  2.  
  3. int main (void)  
  4. {  
  5.     int a[SIZE]={[5]=100,[50]=49};  

 

All other numbers are 0 by default. Let's consider the following code:

 
 
  1. #define SIZE 10  
  2.  
  3. int main (void)  
  4. {  
  5.     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:

 
 
  1. int main (void)  
  2. {  
  3.     int size;  
  4.     int a[size];  
  5.     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.

 
 
  1. #define SIZE 10  
  2.  
  3. int main (void)  
  4. {  
  5.     int a[SIZE];  
  6.     int b[SIZE];  
  7.     int i ;  
  8.     for(i=0;i<SIZE;i++)  
  9.     {  
  10.         a[i]=i;  
  11.     }  
  12.     for(i=0;i<SIZE;i++)  
  13.     {  
  14.         b[i]=a[i];  
  15.     }  
  16.     for(i=0;i<SIZE;i++)  
  17.     {  
  18.         printf("%d",b[i]);  
  19.     }  

 

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.

 
 
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4.  
  5. #define SIZE 10  
  6.  
  7. int main (void)  
  8. {  
  9.     int a[SIZE];  
  10.     int b[SIZE];  
  11.     int i ;  
  12.     for(i=0;i<SIZE;i++)  
  13.     {  
  14.         a[i]=i;  
  15.     }  
  16.     memcpy(b,a,sizeof(a));  
  17.     for(i=0;i<SIZE;i++)  
  18.     {  
  19.         printf("%d",b[i]);  
  20.     }  

 

 

 

This article is from the "kym" blog, please be sure to keep this source http://kymsha.blog.51cto.com/647951/290505

Related Article

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.