赞
踩
C语言中二维字符数组的定义和初始化
一般来说,我们可能会希望定义一个二维字符数组并且在定义的时候就用一些字符串来初始化它。比如说:
Code:
1.char testcase[30][MAX_LENGTH]
= {"jo","vicent","tom","honey","gigi","lily","susan","peter","bob","ron",
2."jason","henry","kiki","ken","auscar","vivian","yiyi","peace","iron","lotus"
3."andy","arta","ophone","denial","pipe","wade","james","kobe","kent","angel"};
通常使用二维字符数组是为了在程序中存储一些字符串关键字。用这样的方式来初始化就最简单不过了。其中,MAX_LENGTH是所有字符串中最大的长度。当然不能忘记'\0'了。
而定义一个字符二维数组,C也提供了简洁的方式,如果我不想统计字符串的长度,我还可以这样定义:
Code:
1.char *testcase[30] = {"jo","vicent","tom","honey","gigi","lily","susan","peter","bob","ron",
2."jason","henry","kiki","ken","auscar","vivian","yiyi","peace","iron","lotus"
3."andy","arta","ophone","denial","pipe","wade","james","kobe","kent","angel"};
也就是说,可以不再数组定义的时候指定字符串的长度,但是注意字符串的个数是一定要指定的,像这样写char **testcase = ...是不行的,不过肯定不行了,因为int *pt=3也不行,呵呵,这两个例子是一个道理啊。
我觉得肯定是人都喜欢第二种初始化方法了,而且它还有一个优点,起码对喜欢用指针的同志来说是一个大优点。就是可以将这样
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。