C 指针和链表

标签 c pointers linked-list

我正在通过使用(单个)链表编写一个简单的 C 员工数据库程序。目前我正在尝试编写下面给出的“删除员工”函数。

我正在尝试使用 while 循环遍历链表,并在当前指针名称字段与用户希望删除的名称匹配时停止。 (存储在 linestore 中。)

出于某种原因,无论如何,它都会一直循环遍历数据库直到结束。我尝试在每个阶段打印 linestore & currptr->name 的内容,它们看起来是正确的,所以我不知道我做错了什么。

如有任何帮助,我们将不胜感激。

删除员工函数:

char *lineptr;
char linestore[300];
lineptr = &linestore;

struct Employee *currptr = root;
struct Employee *prevptr = NULL;

fprintf(stderr, "\nPlease enter the EXACT name of the employee to be deleted.\n");
read_line(stdin, lineptr, MAX_NAME_LENGTH);  //linestore function is working (checked)



while ( (currptr->name != linestore) & (currptr != NULL) )
{
  fprintf(stderr, "\n***Searching database...***\n");
  fprintf(stderr, "***The current record is %s", currptr->name);
  prevptr = currptr;
  currptr = currptr->next;
}

if ( currptr->name == linestore )
{

  fprintf(stderr, "\n***Record DELETED.***\n");
}

最佳答案

currptr->name == linestore

== 不比较值。相反,它比较指针。对于字符串比较,您需要使用 strncmp 函数。

关于C 指针和链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18133340/

相关文章:

c - 从 C 中的结构数组中检索结构以进行 qsort

c - 通过 token 连接重复调用宏是未指定的行为吗?

c - 如何解释 C 程序的输出

c++ - 将指针传递给函数并修改它

c - C 中的空指针比较

algorithm - 返回单向链表尾部(或末尾)的第 k 个元素

c - 我不明白为什么 HIWORD 宏使用 &0xFFFF 掩码?

c - 在 C 中为 3 维数组分配空间

python - arr = [1, 2, 3] 的引用 arr 存储在哪里?

C++ Pop函数链表