【问题标题】:Linked list of chars in CC中的字符链接列表
【发布时间】:2019-05-12 15:06:57
【问题描述】:

我正在尝试创建一个 包含 char 类型数据的链表。

由于某种原因,代码不起作用。 GCC 编译器对函数“add_bottom_ListEl”的警告是

“警告:传递 'add_bottom_listEl' 的参数 2 从指针生成整数而不进行强制转换”

"注意:预期为 'char',但参数类型为 'char *"

我怀疑我使用指针的方式有问题,但我尝试了很多很多组合,将指针传递给函数等......但似乎没有任何效果。

这里是 main 函数和所有其他函数。所有文件中都定义了 MAX_CHAR (#define MAX_CHAR 30)

int main()
{
    char name[MAX_CHAR];
    scanf("%s", name);
    ListEl *head = malloc(sizeof(ListEl));
    strcpy(head->name, name);
    head->next = NULL;
    printf("%s", head->name);
    add_bottom_listEl(head, name);
    print_listEl(head);
    return 0;
}
void add_bottom_listEl (ListEl *head, char name)
{
    ListEl *newEl;
    while(head->next!=NULL)
    {
        head=head->next;
    }
    newEl = (ListEl*) malloc(sizeof(ListEl));
    strcpy(newEl->name, name);
    newEl->next = NULL;
}
void print_listEl(ListEl* head)
{
    puts("print");
    ListEl* current = head;
    while (current!=NULL)
    {
        int i=1;
        printf("%d.%s\n", i, current->name);
        ++i;
        current = current -> next;
    }
}

ListEl结构只是链表的一个常规元素

struct ListEl
{
    char name[MAX_CHAR];
    struct ListEl* next;
};

显然,我用过

typedef struct ListEl ListEl;

互联网或本网站上的每个链表教程都只展示了如何处理一般的整数或数字列表,而不是数组(字符)。有谁能帮帮我吗?

【问题讨论】:

  • void add_bottom_listEl (ListEl *head, char name) - name 应该是 char*
  • 还要确保你有在 main 之前声明的函数
  • 它们在我的代码中分别声明在 .c 和 .h 文件中
  • 在复制名称的函数中,使用 strncpy 传递 MAX_CHAR -1 作为最后一个参数,然后确保使用 [MAX_CHAR-1] = '\0'; 终止名称;

标签: c list pointers linked-list char


【解决方案1】:

如果您在 add_bottom_listEl 中的意图是修改并传回 head,则必须将 head 作为指向指针的指针传递:

    void add_bottom_listEl(ListEl** head, char* name) {
        if ( head == NULL ) {         
    //head is invalid, do nothing
            return;
        }
    //Use calloc instead of malloc to initialise the memory area
        ListEl* newEl = (ListEl*)calloc(1, sizeof(ListEl));
    //Ensure only name of the permissible length is copied
        strncpy(newEl->name, name, MAX_CHAR-1);
    //No need to do this now...calloc will initialise it to NULL
        //newEl->next = NULL;

        if ( *head == NULL ) {
    //No nodes in list yet, this is the first
            *head = newEl;
        } else if ( *head != NULL ) {         
    //Find the end of the list
            while((*head)->next!=NULL) {
                *head = (*head)->next;
            }
        }
    //Add the new node to the list
        *head = newel;
   }

当你调用这个修改版本的函数时,传递指针的地址:

    add_bottom_listEl(&head, name);

您可以通过这样做使您的 typedef 更具可读性:

    typedef struct _listEl {
        char name[MAX_CHAR];
        struct _listEl* next;
    } ListEl;

【讨论】:

  • 工作得很好,编译器中只有一个我不太理解的警告-“警告:从不兼容的指针类型传递'add_bottom_listEl'的参数1”。我就像你写的那样实现了代码。知道这是从哪里来的吗?
  • 你能编辑你的原始帖子并更新你现在拥有的源吗...在Edit下标记它。
  • 当我使用你的最新编辑时,警告没有出现,谢谢
  • 另一个编辑,现在你可以在 main 中删除你的 malloc 并调用函数来初始化列表,在调用之前确保 head = NULL。
【解决方案2】:

您的函数“add_bottom_listEl”采用一个称为“名称”的字符,而不是字符数组(或指向字符的指针)。我猜你希望它是:

add_bottom_listEl(ListEl *head, char *name)

【讨论】:

    【解决方案3】:

    线

    void add_bottom_listEl (ListEl *head, char name)

    应该是

    void add_bottom_listEl (ListEl *head, char* name)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 2021-01-10
      • 1970-01-01
      • 2019-07-03
      • 1970-01-01
      相关资源
      最近更新 更多