怎么定义一个数组元素为字符串的数组
如何定义一个数组元素为字符串的数组?
如题
------解决方案--------------------
方法1, 使用指针数组:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *test[]={ "this is a test ", "test 2 ", " "};
int i=0;
while(strcmp(test[i], " ") != 0)
puts(test[i++]);
system( "PAUSE ");
return 0;
}
这个方法比较简单,
但是问题是这样的话 字符串 是常量,无法修改。
当然这个问题也可以解决,
比如使用数组赋值,
然后将 char 数组首地址赋值给某一个指针即可。
------解决方案--------------------
请使用vector <string> 。标准库内容,规范用法,可以省去很多事情。
如题
------解决方案--------------------
方法1, 使用指针数组:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *test[]={ "this is a test ", "test 2 ", " "};
int i=0;
while(strcmp(test[i], " ") != 0)
puts(test[i++]);
system( "PAUSE ");
return 0;
}
这个方法比较简单,
但是问题是这样的话 字符串 是常量,无法修改。
当然这个问题也可以解决,
比如使用数组赋值,
然后将 char 数组首地址赋值给某一个指针即可。
------解决方案--------------------
请使用vector <string> 。标准库内容,规范用法,可以省去很多事情。