当前位置: 技术问答>linux和unix
写了段代码,发现个问题,求解释
来源: 互联网 发布时间:2017-03-16
本文导语: 本帖最后由 xiaoliu5396 于 2012-09-21 15:24:44 编辑 //test.c #include #include static void command(const char *command, char *cmd[]) { static char buff[30]; int i=0; strcpy(buff, command); cmd[i] = strtok(buff, " "); while (cmd[i] != NULL) { i++...
#include
#include
static void command(const char *command, char *cmd[])
{
static char buff[30];
int i=0;
strcpy(buff, command);
cmd[i] = strtok(buff, " ");
while (cmd[i] != NULL)
{
i++;
cmd[i] = strtok(NULL, " ");
}
}
int main(int argc,char *argv[])
{
if (argc ./aaa 5 6
5 5
5 5
但是在
static void command(const char *command, char *cmd[])
{
static char buff[30];
int i=0;
strcpy(buff, command);
cmd[i] = strtok(buff, " ");
while (cmd[i] != NULL)
{
i++;
cmd[i] = strtok(NULL, " ");
}}
数组越界了。
|
command函数中有问题,问题语句;cmd[i] = strtok(NULL, " ");,应该改成:cmd[i] = strtok(buff+strlen(cmd[i-1], " "));因为你每次的查找都应该基于上次的查找位置