【问题标题】:Pointer to struct "undeclared (first use in this function)"指向结构“未声明(在此函数中首次使用)”的指针
【发布时间】:2025-12-25 01:45:14
【问题描述】:

我正在尝试在 C 中实现一个链表, 而且我很难弄清楚为什么在编译时会出现以下错误:

entryList.c:7:11: error: 'tmp' undeclared (first use in this function)
   entry * tmp = NULL;
entryList.c:7:11: note: each undeclared identifier is reported only once for
   each function it appears in
       ^

我已经为这个程序写了几个链表,它们都使用类似的语法,但编译器只抱怨这个。

我在 header.h 中有我的结构定义:

/*definition of entry type*/
typedef struct entry
{
  char * label;
  short int address;
  struct entry * next;
} entry;

在 entryList.c 中,我正在编写一个函数来将一个节点添加到链表中。

#include "header.h"

static entry * head = NULL;

void addEntry(char * entry, int line)
{
  entry * tmp = NULL;
  char * label = NULL;
  tmp = malloc(sizeof(entry));
  label = malloc(sizeof(char)*MAX_LINE);
  strcpy(tmp->label, entry);
  tmp->address = 0;
  tmp->next = NULL;

  if (!head)
  {
    head = tmp;
  }
  else
  {
    entry * p = head;
    while (p->next)
      p = p->next;
    p->next = tmp;
  }
}

【问题讨论】:

    标签: c linked-list undeclared-identifier


    【解决方案1】:
    void addEntry(char * entry, int line)
    {
        entry * tmp = NULL;
    

    你有一个参数和一个名为entry的类型。将其中一个更改为其他内容。

    【讨论】: