设为首页 加入收藏

TOP

C/C++ 数组的初始化
2013-02-08 14:31:06 】 浏览:632
Tags:C/C   初始

  一致在搞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));

  直接操作内存,搞定之

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C++标准库容器总结 下一篇C++ 如何使用时间函数

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目