如何在C初始化数组为0? [英] How to initialize array to 0 in C?

查看:186
本文介绍了如何在C初始化数组为0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在C大空数组作为一个全球性的。有没有办法做到这一点,除了打字

I need a big null array in C as a global. Is there any way to do this besides typing out

char ZEROARRAY[1024] = {0, 0, 0, /* ... 1021 more times... */ };

推荐答案

全局变量和静态变量自动初始化为零。如果你简单地

Global variables and static variables are automatically initialized to zero. If you have simply

char ZEROARRAY[1024];

在全局范围内它会在运行时全部为零。但实际上有的简写语法,如果你有一个本地阵列。 <一href=\"http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/aryin.htm\">If一个数组部分初始化,未初始化获得相应类型的值0元素你可以这样写:

at global scope it will be all zeros at runtime. But actually there is a shorthand syntax if you had a local array. If an array is partially initialized, elements that are not initialized receive the value 0 of the appropriate type. You could write:

char ZEROARRAY[1024] = {0};

,编译器将用零填充不成文的条目。另外,您可以使用 memset的来数组在程序启动时初始化:

The compiler would fill the unwritten entries with zeros. Alternatively you could use memset to initialize the array at program startup:

memset(ZEROARRAY, 0, 1024);

如果你已经改变了它,想回重置为全零,这将是有益的。

That would be useful if you had changed it and wanted to reset it back to all zeros.

这篇关于如何在C初始化数组为0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆