ITeye博客

ios开发答疑录系列​---(三)C/C++ 数组的初始化

mmdev · 2012-12-01 01:27 · Markdown

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

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

蓝岩--移动互联网老兵

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

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

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

成员全部初始化:

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

将缺省的成员置为 0:

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

初始化全部成员为0:

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

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

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

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

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));

直接操作内存,搞定之

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。