Array of C languages

Source: Internet
Author: User
Tags array length

Array 1. Storage of arrays in memory

An array is a collection of some kind of data that is stored continuously in memory, with no gaps between adjacent data.

Some type: It can refer to the basic data type, road int type, float type, char type, double type, and so on, also can be a pointer,

If it is a pointer, then the pointer can point to a basic data type, such as a pointer to an int type, a double type, and so on, to an array, or to a function

The following is a simple example of an array (an array of type int):

Declares an array of type int of 5 elements (uninitialized)---

int arr[5];

So the data of these 5 int types are stored in memory like this (for the sake of distinguishing, using different colors to label the table, the inside of each square ...) Indicates undefined value):

In the machine, in general, an int type of data occupies 4 bytes of space, so 5 int data requires 20 bytes of memory space.


Here are two concepts that need to be differentiated:

The length of the array and the length of the memory space occupied by the group

The length of the array is the number of elements in the array, and our example--int arr[5]--arr is 5.

However, the length of the memory space of the array arr in memory is not 5, it should be 5 x 4 = 20 bytes

The following code is used to calculate the number of bytes in memory of the array:

int Main () {    int arr[5];    printf ("%d\n"sizeof(arr));     return 0 ;}

Program Output Result:

So how do you calculate the number of array elements?

You can use an array of bytes in memory to remove the number of bytes per element =, which is the number of elements:

printf ("%d\n"sizeof(arr)/sizeof(int));

2. Initialization of arrays

Once the array is stored in memory, it is necessary to assign a value to each element in the array, so that the initialization of the array has some methods:

1.

int arr[5] = {12345};

At the time of declaration, give the initialization value of each element

2. Partial initialization

int arr[5] = {123};

5 elements are declared, but only 3 values are given, note that these three values are the values of the first three elements of the array arr, and element 4 and element 5 will be initialized to the default value of 0 by the compiler.

3. The length of the array is not given

int arr[] = {12345};

This initialization method, the compiler will default array length is 5

Array of C languages

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.