读书人

字符串长度,该如何处理

发布时间: 2013-06-26 14:29:32 作者: rapoo

字符串长度

#include<stdio.h>
int main(void)
{
const char *mytal[5]={"adding numbers swiftly",
"multiplying accurately",
"stashing data",
"following instructions to the letter",
"understanding the c language"};
printf("%s\n",*mytal[3]);
char mytal_2[5][]={"adding numbers swiftly",
"multiplying accurately",
"stashing data",
"following instructions to the letter",
"understanding the c language"};
printf("%c\n",mytal_2[5][10]);
return 0;
}

其实我想问的是,上面指针数组字符串长度由初始化字符串决定,那二维数组可不可以,因为我不知道字符串中要放多少字符,char mytal_2[5][]这种形式可不可以 二维数组 指针数组
[解决办法]
printf("%s\n",*mytal[3]); 打印串,得是个地址,不能有*
printf("%c\n",mytal_2[5][10]); 5长度的数组,下标最大为4,越界了。
字符数组需要先知道长度。mytal_2[5][] 是不可以的,如果不知道长度,可以定义指针数组,然后使用malloc动态分配内存

读书人网 >C语言

热点推荐