---
title: "ios开发答疑录系列\u200b---(三)C/C++ 数组的初始化"
author: "mmdev"
date: "2012-12-01T01:27:00Z"
tags: []
slug: "ios-c-c"
draft: false
lastmod: "2013-01-05T09:48:43Z"
id: 1761012
url: "https://mmdev.iteye.com/blog/1761012"
---

============================================================

博文原创，转载请声明出处

[蓝岩--移动互联网老兵](http://blog.csdn.net/shencaifeixia1)

============================================================

一致在搞ObjC，使用NSArray, 突然用到c的数组，有些陌生，复习一下。

c数组初始化有以下几种方式，

成员全部初始化：

```c
int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
```

将缺省的成员置为 0:

```c
int myArray[10] = { 1, 2 }; //initialize to 1,2,0,0,0...
```

初始化全部成员为0:

```c
int myArray[10] = { 0 }; //all elements 0
```

在 C++中, 空的初始化也会将全部成员置为 0:

```c
int myArray[10] = {}; //all elements 0 in C++
```

如果声明为static，则全部成员默认为0，如果你不去手动初始化的话。

```c
static int myArray[10]; //all elements 0
```

\----------

然后我在开发中遇到问题，如果我的数组为类成员，我不想声明static，如下：

@private int touchInfos\[2];

这时候初始化遇到问题，一下方法都编译不过。

```
 int tis[2]={0,0};
    touchInfos=tis;
```

```
    touchInfos={0,0};
```

只能去一个一个的赋值，one by one =\_=,后来想到办法，用memset:

```
memset(touchInfos, 0, sizeof(touchInfos));
```

直接操作内存，搞定之