【发布时间】:2020-09-06 03:40:51
【问题描述】:
我正在编写这个程序来将字符串转换为 C 中的字符串数组(使用 char**)。但似乎程序只存储第一个值的元素,而其余的只是打印(null)。我可以知道我在这里做错了什么吗?
这是我如何测试我的代码的 sn-p。我正在为 char** 分配内存,并将其传递给 toStrArray 函数以将 argv[1] 中的每个字符串存储到 arg_1(由分隔符“”分隔)。 这意味着每次程序看到一个“”,它就标志着停止并将字符串存储到数组元素中
预期输出:这座城市很漂亮!但实际输出:this (null) (null) (null)
int main( int argc, char *argv[] )
{
int i;
int numWords; /* Pointer size (row) for arg_1 */
char **arg_1; /* String array for argument 1 */
if( argc == ARGUMENT_SIZE )
{
numWords = getNumWords(argv[1]); /* Get the number of words in arg_1
eg: "This city is beautiful" will return 4 */
/* Allocation of string array of arg_1 */
arg_1 = (char**)malloc(numWords * sizeof(char*));
/* ASSERTION: Iterate through the rows of string array and allocate their memory */
for( i = 0; i < numWords; i++ )
*arg_1 = (char*)malloc(STR_LENGTH * sizeof(char));
arg_1 = toStrArray( argv[1], arg_1, " " ); /* Converts each word in argv[1] to separated string array
so that we can find matching with argv[2] and change it */
for( i = 0; i < numWords; i++ )
printf("%s ", arg_1[i]);
}
return 0;
}
这是我的 toStrArray
char** toStrArray(char str[], char **strArr, char delim[])
{
char *token;
int i;
i = 0;
token = strtok(str, delim);
/* ASSERTION: Token the string until the end of the string */
while( token != NULL )
{
strArr[i] = token; /* Assigning each word into element array */
token = strtok(NULL, delim); /* Get the next token (word) */
i++;
}
return strArr;
}
getNumWords 函数:
int getNumWords(char str[])
{
int num_words;
char *token;
token = strtok(str, " ");
num_words = 0;
/* ASSERTION: Iterate until we reach the last token */
while( token != NULL )
{
token = strtok(NULL, " ");
num_words++;
}
return num_words;
}
【问题讨论】:
-
你的字数太少了,不是吗?第一个
strtok()找到一个单词,但您将计数设置为 0,而不是 1。 -
*arg_1 = (char*)malloc(STR_LENGTH * sizeof(char));仅分配arg_1中的第一个指针(每次覆盖先前的指针地址时都会一遍又一遍地创建内存泄漏)。在C语言中,malloc的返回不需要强制转换,没有必要。请参阅:Do I cast the result of malloc?。此外,sizeof(char)被定义为1,使得乘法中的包含变得多余。 -
@CalmenChia 每次调用
malloc时,都会将其分配给*arg1,这会覆盖之前分配给它的内存。这会造成内存泄漏,因为以前的内存从未被释放。这也不是你想要的。您希望将每个新分配的内存分配给字符串数组中的下一个位置arg1。因此,您应该在分配新分配的内存时增加该指针。
标签: c pointers command-line null strtok