C/C ++ array variable declaration memo

Source: Internet
Author: User

People who are used to. net and java are not used to using arrays of C/C ++. Arrays and pointers in C/C ++ are equivalent, but they are slightly written.

1. Parameters

For example, an array is used as an input parameter in two ways:

[Cpp]
Int FindMax1 (int * Array );
Int FindMax2 (int Array []);

Compilation is successful, and the semantics is the same.
However, in array initialization:

[Html]
// Invalid int * Array1 = {0, 1, 2, 3 };
Int Array [] = {0, 1, 2, 3}; // pass

Note that the order in which * and [] are combined with int is different.

2. Parameters

In addition, initializing an array in the form of int ArrayName [] can improve the efficiency of array variable initialization.

[Cpp]
Int Array1 [10] = {0}; // initialize the entire array member element to 0;
Char Array2 [] = {'A', 'B', 'C', 0}; // The size is not specified when the array is defined. When the array is initialized, the list is initialized, the array size is determined by the number of list elements during initialization. The array length is 4.
Int Array3 [8] = {1, 2, 3, 4}; // when the declared length of the array is greater than the length of the list element, the remaining element is initialized to 0, equivalent to int v5 [8] = {1, 2, 3, 4, 0, 0, 0 };
3, sizeof ()

Sizeof (int *), the number of bytes of the array pointer. The 32-bit system is 4;

Int Array1 [10] = {0 };

Sizeof (Array1), the actual memory space occupied by the array, here is 4*10 = 40;

Summary:

Int * Array1 indicates the pointer, while int Array1 [] indicates the array.


In the column from bestwolf1983
 

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.