C/C ++ array Initialization

Source: Internet
Author: User

We are working on ObjC, using NSArray, And suddenly using arrays of c. It's a little strange. Let's review it.

There are several methods to initialize the c array,


Initialize all members:

Int myArray [10] = {5, 5, 5, 5, 5, 5, 5, 5, 5 };
Set the default member to 0:

Int myArray [10] = {1, 2}; // initialize to 1, 2, 0, 0...
Initialize all Members as 0:

Int myArray [10] = {0}; // all elements 0
In C ++, empty initialization also sets all Members to 0:

Int myArray [10] ={}; // all elements 0 in C ++
If the declaration is static, all members are 0 by default. If you do not perform manual initialization.

Static int myArray [10]; // all elements 0

----------

Then I encountered a problem during development. If my array is a class member, I don't want to declare static, as shown below:
@ Private int touchInfos [2]; www.2cto.com

At this time, there is a problem with initialization, and the methods are not compiled.

[Cpp]
Int tis [2] = {0, 0 };
TouchInfos = tis;
[Cpp]
TouchInfos = {0, 0 };
Only values can be assigned one by one. one by one = _ =. Later, I thought of a solution using memset:
[Cpp]
Memset (touchInfos, 0, sizeof (touchInfos ));
Directly operate on the memory.

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.