C/C++ 数组的初始化

2013-02-08 14:31:06 · 作者: · 浏览: 443

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

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

  成员全部初始化:

  int myArray = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

  将缺省的成员置为 0:

  int myArray = { 1, 2 }; //initialize to 1,2,0,0,0…

  初始化全部成员为0:

  int myArray = { 0 }; //all elements 0

  在 C++(www.cppentry.com)中, 空的初始化也会将全部成员置为 0:

  int myArray = {}; //all elements 0 in C++(www.cppentry.com)

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

  static int myArray ; //all elements 0

  ----------

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

  @private int touchInfos ;

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

  [cpp]

  int tis ={0,0};

  touchInfos=tis;

  [cpp]

  touchInfos={0,0};

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

  [cpp]

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

  直接操作内存,搞定之