Talk C chestnuts together (91st back: C language instance -- array size)

Source: Internet
Author: User

Talk C chestnuts together (91st back: C language instance -- array size)
Hello, everyone. In the previous session, we talked about the example of using pipelines for inter-process communication. This example is as follows:Array size. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

Recently, the viewer has encountered an issue about the array size. I will make a special description here, hoping to help you.

Generally, the sizeof operator is used to calculate the array size, so it is used directly without thinking. Sometimes, if you want to use the number of elements in the array in the program and the result is calculated using sizeof, a problem occurs, but you do not know where the problem is. The sizeof operator is not fully understood.

Let's first describe two concepts:Array size and number of elements in the array.

Array size: The size of memory occupied by the index group. Number of elements in the array: Number of elements in the index group.

First, you need to knowThe sizeof operator calculates the memory size.. When we use sizeof to operate the array, we calculate the memory space occupied by the array, that is, the size of the array. For example:

The result of sizeof (int) is 4 (related to the compiler and the specific machine), indicating that the int type value occupies 4 bytes of memory space. The result of sizeof (array) is 8, indicating that the array occupies 8 bytes of memory space.

After understanding the above example, we can use the size of memory space occupied by the entire array divided by the size of memory space occupied by a single element in the array. The result is the number of elements in the array. The following is an example:

#include
  
   #include
   
    #define SIZE 7typedef struct person{    int age;    char *name;    char flag;}Person;int main(){    int array1[SIZE];    char array2[SIZE];    Person array3[SIZE];    printf("the size of array1 is %d ,and it have %d elements \n",sizeof(array1),sizeof(array1)/sizeof(int));    printf("the size of array2 is %d ,and it have %d elements \n",sizeof(array2),sizeof(array2)/sizeof(char));    printf("the size of array2 is %d \n",strlen(array2));    printf("the size of array3 is %d ,and it have %d elements \n",sizeof(array3),sizeof(array3)/sizeof(Person));    return 0;}
   
  

Compile and run the above program. The following result is displayed:

The size of array1 is 28, and it have 7 elements // the size of array2 is 7, and it have 7 elements // array of the char type the size of array2 is 7 // array of the char type is processed as a string the size of array3 is 84, and it have 7 elements // array of the struct type

In the preceding example, three types of arrays are used. We can see that different types of arrays occupy different memory spaces, but the number of elements in the array is the same, because we use a macro when defining an array.

In addition, the size of the char array is the same as the number of elements in the array, because char variables on my computer occupy one byte of memory space. In addition, this type of array may also be processed as a string. In this case, the strlen function can be used to obtain the size of the array.

When writing a program,Do not confuse the array size with the number of elements in the array. They are two different concepts, and their calculation methods are also different.

If you want to know the size of the array, you can directly use sizeof to operate the array. To use the number of elements in the array, use the division operation described in the preceding example.

Let's talk about the array size example. For more information, see the following example.

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.