【问题标题】:Structure node pointers assignment结构节点指针赋值
【发布时间】:2020-05-09 01:23:33
【问题描述】:

我有一个包含结构节点的作业,在其中一个函数中,写的是 current->next->Firstname,我的问题是: current->next->Firstname 的数据类型是什么? 谢谢 . 代码的相关部分如下:

typedef struct node
{
    char Lastname[50];
    char Firstname[50];
    char Initials[50];
    char Mobile[50];
    char Class[50];
    char InitialSort[50];
    char RandomSort[50];

    struct node *next;
} node;

node *HEAD=NULL;

// more code here.

void sortedInsert(node** head_ref,node* new_node)
{
    node* current;
    // Special case for the head end
    if (*head_ref == NULL || (*head_ref)->Firstname >= new_node->Firstname)
    {
        new_node->next = *head_ref;
        *head_ref = new_node;
    }

    else
    {
        // Locate the node before the point of insertion
        current = *head_ref;
        while (current->next!=NULL && strcmp(current->next->Firstname, new_node->Firstname)<0 )
        {
            current = current->next;
        }
        new_node->next = current->next;
        current->next = new_node;
    }
}

【问题讨论】:

  • 你的问题有点不清楚。 “具有类似说选项的数据类型”。什么是“选项”?它没有显示在您的代码中,也不是本机数据类型。
  • @Jeroen Van de Kaai 至于我,我还没有理解这个问题。
  • 如果 option = current->next->Firstname,我应该如何删除选项? int, char char* , node* 等
  • 你们,我只有 7 点声望点,你却把我击倒了。我试图让我的问题尽可能清楚。给新人一个机会...
  • char *option = current-&gt;next-&gt;Firstname。但请注意,option 将成为指向原始数据的指针。这不是副本。

标签: c sorting pointers linked-list structure


【解决方案1】:

任何数组都是隐式指向第一个元素的指针。所以 char Firstname[50] 只是分配了 50 个字节的 char* Firstname。您的类型将是 char*。

要遵循指针,请理解结构只是一大组分配的数据。所以 current->next 只是读取列表中下一个节点的地址。从那里它要求找到 Firstname 变量。这将跳转到“下一个”节点的 Firstname 的第一个元素的地址。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    相关资源
    最近更新 更多