gpt4 book ai didi

c - 结构体节点指针赋值

转载 作者:行者123 更新时间:2023-11-30 16:06:45 30 4
gpt4 key购买 nike

我有一个作业,其中包含结构节点,并且在其中一个函数中,写为 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;
}
}

最佳答案

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

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

关于c - 结构体节点指针赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59867256/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com