我正在尝试标记通过的字符串,将标记存储在数组中并返回它。for循环仅用于验证字符串是否实际上已被拆分为标记,请忽略。
样本输入:coinflip 3
我的代码思考过程如下:
take: string
if string = null: return null
else:
while temp != null
token[i++] = temp
temp = get next token
return
这是我当前的解决方案。分隔符是空格。一段时间以来,C并不是我的强项。
#include <stdio.h>
#include <string.h>
//Clears the screen and prompts the user
//Read-in string
char *readIn(void)
{
char param[101];
fgets(param, 101, stdin);
return param;
}
//parse string
char *parseString(char* cmd)
{
char* temp = strtok(cmd, " ");
if (cmd == NULL)
{
return temp;
}
else
{
int i = 0;
char *tokens[3];
while (temp != NULL)
{
tokens[i++] = temp;
temp = strtok(NULL, " ");
}
for (i = 0; i < 3; i++)
{
printf("%s\n", tokens[i]);
}
return tokens;
}
}
//Command
int command()
{
return 1;
}
int main()
{
int term = 1;
char* cmd;
char *parS;
while (1)
{
//prompt
msg();
//read
cmd = readIn();
//parse
parS = parseString(cmd);
//command
term = command(parS);
if (term == 0)
{
break;
}
}
return 0;
}
当前错误:分段错误(核心已转储)
编译器已经报告该功能
//Read-in string
char *readIn(void)
{
char param[101];
fgets(param, 101, stdin);
return param;
}
具有未定义的行为,因为它返回指向本地数组param
的指针,该数组在退出函数后将不活动。
在此功能中
char *parseString(char* cmd)
{
char* temp = strtok(cmd, " ");
if (cmd == NULL)
{
return temp;
}
else
{
int i = 0;
char *tokens[3];
while (temp != NULL)
{
tokens[i++] = temp;
temp = strtok(NULL, " ");
}
for (i = 0; i < 3; i++)
{
printf("%s\n", tokens[i]);
}
return tokens;
}
}
存在相同的问题(如果不考虑错误的实现),并且返回表达式的类型
return tokens;
与函数的返回类型不对应,因为return语句中的表达式的类型为char **
,而函数的返回类型为char *
。
我相信对您来说最困难的是编写将字符串拆分为标记的函数。
它看起来像下面的演示程序中所示的以下方式。该函数为指向令牌的指针数组动态分配内存。如果分配失败,函数将返回NULL。否则,该函数将指针返回到动态分配的指针数组的第一个元素。数组的最后一个元素包含NULL。此元素可用于确定数组中标记的实际指针数。
您在这里。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char ** parseString( char *cmd )
{
char **tokens = malloc( sizeof( char * ) );
*tokens = NULL;
size_t n = 1;
const char *delim = " \t";
char *p = strtok( cmd, delim );
int success = p != NULL;
while ( success )
{
char **tmp = realloc( tokens, ( n + 1 ) * sizeof( char * ) );
if ( tmp == NULL )
{
free( tokens );
tokens = NULL;
success = 0;
}
else
{
tokens = tmp;
tokens[n - 1] = p;
tokens[n] = NULL;
++n;
p = strtok( NULL, delim );
success = p != NULL;
}
}
return tokens;
}
int main(void)
{
char cmd[] = "Many various and unique commands";
char **tokens = parseString( cmd );
if ( tokens != NULL )
{
for ( char **p = tokens; *p != NULL; ++p )
{
puts( *p );
}
}
free( tokens );
return 0;
}
程序输出为
Many
various
and
unique
commands
嗯,除了该代码不能处理不超过3个令牌这一事实,它还有另一个基本问题:它将返回非法的指向内存的指针。 temp
和tokens
是在parseString()
功能的堆栈框中的变量。因此,当执行完成时,这些变量将消失。理想的解决方案是在堆中分配tokens
。
这是我的解决方案:
char** parseString(char* cmd)
{
char delimiters[] = " ";
char* temp = strtok(cmd, delimiters);
//If temp is NULL then the string contains no tokens
if (temp == NULL)
{
return NULL;
}
else
{
int i = 0;
char** tokens = malloc(3*sizeof(char*));
while (temp != NULL)
{
tokens[i++] = temp;
temp = strtok(NULL, " ");
}
for (i = 0; i < 3; i++)
{
printf("%s\n", tokens[i]);
}
return tokens;
}
}