【问题标题】:Why String Array only get to return first element, but not the subsequent element?为什么字符串数组只返回第一个元素,而不返回后续元素?
【发布时间】: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


【解决方案1】:

您不能在内存中的同一个字符数组上使用两次strtok。输入

char str = {'T', 'h', 'i', 's', ' ', 'c', 'i', 't', 'y', ' ', 'i', 's',
            ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l', '!',  0};

会变成

char str = {'T', 'h', 'i', 's',   0, 'c', 'i', 't', 'y',   0, 'i', 's',
              0, 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l', '!',  0};

执行后

char* ret1 = strtok(str,  " "); // Sets the null byte after 'This'
char* ret2 = strtok(NULL, " "); // Sets the null byte after 'city'
char* ret3 = strtok(NULL, " "); // Sets the null byte after 'is'
char* ret4 = strtok(NULL, " "); // Doesn't modify str

请注意,每个空白字符都替换为终止空字节。这样做会将输入字符串切割成多个子字符串:

printf("%s\n", ret1); // Outputs 'This'
printf("%s\n", ret2); // Outputs 'city'
printf("%s\n", ret3); // Outputs 'is'
printf("%s\n", ret4); // Outputs 'beautiful!'

如果您调用toStrArray,这已经发生了,并且使用argv[1] 调用strtok 返回NULL,因为子字符串“This”不包含分隔符。

因此我建议用strchr 来计算分隔符的数量:它返回一个指向字符第一次出现的指针。从那里继续搜索,直到找不到更多空格,即返回 NULL。

#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "This city is beautiful!";

    int count = 0;
    for(char* tmp = str; tmp; count++) {
        while (tmp[1] == ' ')     // Ignore preceding and multiple whitespaces
            tmp++;
        tmp = strchr(tmp+1, ' '); // Find next whitespace
    }

    printf("Number of words: %d\n", count);
}

【讨论】:

  • 注意字符串中的多个空格 (This city is beautiful)。使用交错的strcspn()strspn() 调用可能会做得更好——strcspn() 查找标记,strspn() 查找空白序列。请记住,strtok() 会跳过多次出现的分隔符。
  • 第二个for循环条件“tmp;”在 for(char* tmp = str;tmp;count++)。是否等同于 tmp != NULL?
  • @CalmenChia 是的,它是:由于 C 中没有布尔数据类型,因此任何非零表达式都被解释为 true。所以tmptmp != NULL 的简写。
  • @JonathanLeffler 感谢您的评论。我添加了一些代码来拦截前面和多个空格。尾随的不考虑在内,但我想保持简单。
猜你喜欢
  • 2021-09-20
  • 1970-01-01
  • 2018-11-30
  • 1970-01-01
  • 2011-07-12
  • 2012-04-10
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
相关资源
最近更新 更多