【问题标题】:Create a function that returns an array in which each cell contains the address of a string (representing a word)创建一个函数,该函数返回一个数组,其中每个单元格都包含一个字符串的地址(代表一个单词)
【发布时间】:2020-03-19 14:40:37
【问题描述】:

我需要一些关于这个编码练习的建议:

编写一个将字符串拆分为单词的函数。分隔符将全部 是非字母数字字符。该函数返回一个数组,其中 每个单元格都包含一个字符串的地址(代表一个单词)。这 最后一个单元格必须为 NULL 才能终止数组。

我需要帮助的两件事是: 我的函数如何正确地将数组返回到我的主函数 malloc 数组是否使用得当?

我想我错过了指针部分,因为在编译过程中出现了错误。 如果您有任何想法,请告诉我,谢谢!

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

char **my_str_to_word_array(const char *str)
{
    char* tab_address = NULL;
    int j = 0; /*Nombre de mots*/
    int i = 0;
    char ptr = 0;

    for( i = 0; str[i] != " "; i++)/*Compter le nombre de mots dans la string*/
        if(*str[i] = " ")
            j++;

    tab_address = malloc(sizeof(char) * j); /*tab avec tous les mots compter par j*/
    if (tab_address == NULL)
        exit(0);

    for ( i = 0; str[i] != " "; i++) /*Stocker les adresse dans un tableau */
        if(str[i] = " ")
        {
            i++;
            tab_address[j] = ptr;
        }
        else if (*str[i] = "\0")
            break;

    return ptr;
    free(tab_address);
}

int main(void)
{
    char str[50] = "Hello world";
    my_str_to_word_array(str);

    return 0;
}

【问题讨论】:

  • 我认为您希望在 malloc 中使用 sizeof(char*),因为您为 char 指针分配空间并不简单 chars。 tab_address 应该是 char** 并且你想返回这个而不是 ptrreturn ptr; 之后的任何代码都死了 -> 不会执行。
  • 请编辑代码并修正缩进。

标签: c arrays pointers memory malloc


【解决方案1】:

它似乎工作。请注意,代码假定 str 不是 NULL

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h> // added for usage of isalpha function

char **my_str_to_word_array(const char *str)
{
    char** tab_address = NULL; // FIXED: this need to be an array of char* so char**
    int j = 0; /*Nombre de mots*/
    int i = 0;
    //char ptr = 0; // FIXED: This not needed

    for(i = 0; str[i] != '\0'; i++) // FIXED: you want to go until the end of the string
        if(isalpha(str[i])) // FIXED: you want to check for non alphanumeric here not space
            j++;

    tab_address = malloc(sizeof(char*) * (j + 1)); // FIXED: you want to allocate char* -s not char -s
    if (tab_address == NULL)
        exit(0);

    tab_address[0] = str; // ADDED: the first word should be at the start of the string
    j = 1; // FIXED: reinitialize j to 1
    for ( i = 0; str[i] != '\0'; i++) // FIXED: you want to go until the end of the string 
        if(!isalpha(str[i])) // FIXED: you want to check for non alphanumeric here not space
            tab_address[j++] = &str[++i]; // setting the next pointer to the next words address
        //else if (*str[i] = "\0")  // FIXED: these can be deleted
        //    break;

    tab_address[j] = NULL; // ADDED: setting the last element to NULL
    return tab_address; // FIXED: you want to return the char** array not ptr
    // free(tab_address); // FIXED: this is dead code, and you want to free it later anyway
}

int main(void)
{
    char str[50] = "Hello There World";
    char ** result = my_str_to_word_array(str);
    char ** for_freeing = result; // save the start address of the result to later free it

    while (*result != NULL)
    {
        printf("%s\n", *result);
        result++;
    }

    free(for_freeing);

    return 0;
}

输出

Hello There World
There World
World

如果你仔细想想,这实际上是正确的。

【讨论】:

  • 感谢您的编辑。每行打印一个单词并在每个新单词处返回一行会很棒。
  • 您可以这样做以打印onlinegdb.com/ByQoBB-88。如果你想在字符串中插入新行,那会更麻烦。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-01
  • 2018-12-20
  • 1970-01-01
  • 1970-01-01
  • 2013-07-10
相关资源
最近更新 更多