当前位置:   article > 正文

c语言中定义字母二维数组,C语言中二维字符数组

二维字符数组

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也不行,呵呵,这两个例子是一个道理啊。

我觉得肯定是人都喜欢第二种初始化方法了,而且它还有一个优点,起码对喜欢用指针的同志来说是一个大优点。就是可以将这样

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/319754
推荐阅读
相关标签
  

闽ICP备14008679号