码迷,mamicode.com
首页 > 其他好文 > 详细

usage of char array which elements are mutilple STRINGs ended with a "\0"

时间:2015-04-19 08:57:54      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

usage of char array which elements are mutilple STRINGs ended with a "\0".

#include <stdio.h>


char stringsarray[] = {
        "many" "strings" "\0"
        "are" "stored" "\0"
        "in a strings_array" "\0"
};


void main(void)
{
        //char *ptr = stringsarray;
        printf("the first element is: %s\n", &stringsarray[0]);
        printf("the second element is: %s\n", &stringsarray[1]);
        printf("the third element is: %s\n", &stringsarray[2]);
        printf("the fourth element is: %s\n", &stringsarray[3]);
        printf("the fifth element is: %s\n", &stringsarray[4]);
//      printf("the sixth element is: %s\n", &stringsarray[5]);
//      printf("the seventh element is: %s\n", &stringsarray[6]);
//      printf("the eighth element is: %s\n", &stringsarray[7]);
}

execution result(compiling with gcc):

book@book-desktop:~/s3c2440/clanguage/strings$ gcc -o exe strings.c
book@book-desktop:~/s3c2440/clanguage/strings$ ./exe
the first element is: manystrings
the second element is: anystrings
the third element is: nystrings
the fourth element is: ystrings
the fifth element is: strings
conclusions:

1. statement "printf("the first element is: %s\n", &stringsarray[0]);", it will print continuously string"strings" until arriving "\0" when finishing printing the first string("many") in STRING0("many" "strings" "\0"). (every strings(constant) are appended with a ‘\0‘ as a end symbol by system, thinking in this way, the result of this statement should be "many" only, the part after "many" at the line not include.)

2. there is doubt how to refer to the second line as a part like refering to the first line in a simple way?(please proficients leave direction, thanks!)

there is a such construction in u-boot:

static uchar default_environment[] = {
#if defined(CONFIG_BOOTARGS)
	"bootargs=" CONFIG_BOOTARGS "\0"
#endif
#if defined(CONFIG_BOOTCOMMAND)
	"bootcmd=" CONFIG_BOOTCOMMAND "\0"
#endif
#if defined(CONFIG_RAMBOOTCOMMAND)
	"ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
#endif
#if defined(CONFIG_NFSBOOTCOMMAND)
	"nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
#endif
#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
	"bootdelay=" MK_STR (CONFIG_BOOTDELAY) "\0"
#endif
.............................................
};

来自百度知道:

c语言中 unsigned char default_environment[] = { "baudrate=" "115200" "\0"}; 这怎么解释?

C提供了一种字符数组初始化功能,即如果将要对字符数组初始化为几个字符串常量连接起来的时候,可以在右值的{}中直接按顺序写上这几个字符串常量,编译器会把它们自动衔接起来。字符串常量间可以用空格隔开,也可不用隔开。














usage of char array which elements are mutilple STRINGs ended with a "\0"

标签:

原文地址:http://blog.csdn.net/decisiveness/article/details/45116897

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!