在定义数组的时候,我们可以很方便地初始化数组的元素为 0 值;
例如:
char buf2[12] = {0};
此时,buf2[] 中的所有元素都赋值为 0 值;
//===================================================================
那么,如果我们想给数组的所有元素,都赋值为 0xff 值,如下:
char buf2[12] = {0xFF};
此时,是错误的,这样的操作,只能够给数组的第一个元素设置为 0xFF 值;
然后,其他元素设置为 0x00 值;
//========================================================================
如下是测试的例子;
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void print(char* buf, int len)
{
int i;
for(i = 0; i < len; i++)
{
printf("[%d]%x ", i, buf[i]);
}
printf("\n");
}
//========================================================
//========================================================
int main (int argc, const char * argv[])
{
char buf1[12];
char buf2[12] = {0};
char buf3[12] = {0x00};
char buf4[12] = {0xFF};
char buf5[12] = {0xFF, 0xee};
print(buf1, 12);
print(buf2, 12);
print(buf3, 12);
print(buf4, 12);
print(buf5, 12);
}
//========================================================================
只需要99元就可以投资自己,掌握Linux系统编程!主要传授《Linux C/C++程序设计》《Linux 系统编程》,主页有学习视频!
只需要99元就拥有全部课程和资料,让我们用技术改变自己,改变生活,改变世界!
视频可以百度搜索:韦凯峰Linux编程学堂