【问题标题】:'list' undeclared (first use in this function)'list' 未声明(在此函数中首次使用)
【发布时间】:2012-05-07 03:49:15
【问题描述】:

我有以下代码。 我收到'list' undeclared (first use in this function) 的错误。 请帮帮我

#include <stdio.h>
#include <stdlib.h>
struct list{
int data;
struct list *next;
};
typedef struct list *head;

int main()
{
    struct list *start;
    int i;

    start = (list *) malloc(sizeof(struct list));
    printf("\nEnter the data : \n");
    scanf("%d", &i);
    start->data = i;
    start->next = NULL;
    while(list->next != NULL)
    {
        printf("%d ", list->data);
        list = list->next;
    }

    return 0;
}

【问题讨论】:

  • 你不必发布截图...
  • @ThiefMaster 比什么都不发好(没有代码,没有错误日志)..
  • 评论: 1.不要投malloc()的返回值; 2. 不要使用sizeof struct 作为它的参数,而是使用sizeof(*start); 3. 为什么typedef struct list *head你从来不用它?

标签: c


【解决方案1】:

您正在使用类型 list 而不是变量名称 start。正确的代码:

while (start->next != NULL)
{
    start = start->next;
    // etc.
}

【讨论】:

  • 非常感谢...是的,我做错了
【解决方案2】:

Don't cast the return type of malloc - 没有任何好处,在这种情况下你做错了!

start = (list *) malloc(sizeof(struct list));

应该是

start = malloc(sizeof(struct list));

list * 类型不存在;你的意思是struct list *

您可以通过编写使其更加安全

start = malloc(sizeof(*start));

这样你会自动mallocstart 的(指针)类型提供足够的字节,这在你以后更改start 的类型时很有用 - malloc 调用不会改变一点。

【讨论】:

    【解决方案3】:
    start = (struct list *) malloc ...
    

    您在投射时错过了 struct。 正如 Anthales 指出的那样,这根本没有必要。

    start = malloc ...
    

    【讨论】:

    • 看在上帝的份上,不要强制转换 malloc 的返回类型!停止这种 C++ 疯狂!
    【解决方案4】:

    start = (list *) malloc(sizeof(struct list));
    

    包括不必要的类型转换。做吧

    start = malloc(sizeof(struct list));
    

    但是,您的代码比这更麻烦。我可以通过提出我自己的问题来最好地回答您的问题:在您看来,list 是类型还是对象?

    如果您回答了这个问题,就会有人怀疑您可以修复您的代码。祝你好运。

    【讨论】:

      【解决方案5】:
      #include <stdio.h>
      #include <stdlib.h>
      typedef struct list{
          int data;
          struct list *next;
      } list;
      
      typedef struct list *head;
      
      int main()
      {
          struct list *start;
          int i;
      
          start = (list *) malloc(sizeof(struct list));
          printf("\nEnter the data : \n");
          scanf("%d", &i);
          start->data = i;
          start->next = NULL;
          while(start->next != NULL)
          {
              start = start->next;
          }
      
          return 0;
      }
      

      你可以定义类型(列表*)

      【讨论】:

        【解决方案6】:

        你的变量被命名为“start”,你称之为“list”。

        【讨论】:

          【解决方案7】:

          您遇到的一个问题是,在创建 typedef struct list *start; 之后,您在声明 struct 指针时重用了 struct。此外 struct 和 typedef 不能具有相同的名称。你得到这个:

          cc  -Wall test.c -o test
          test.c: In function ‘main’:
          test.c:13: error: ‘list_t’ undeclared (first use in this function)
          test.c:13: error: (Each undeclared identifier is reported only once
          test.c:13: error: for each function it appears in.)
          test.c:13: error: ‘start’ undeclared (first use in this function)
          test.c:13: error: ‘cur’ undeclared (first use in this function)
          test.c:13: warning: left-hand operand of comma expression has no effect
          test.c:16: error: expected expression before ‘)’ token
          

          您可以选择在任何地方使用 struct list 并跳过使用 typedef 的制作。使用 typedef 可以简化代码的读取方式,如下所述:http://en.wikipedia.org/wiki/Struct_%28C_programming_language%29#typedef

          我已经重写了你刚才的内容,以便我可以编译它并更好地理解它,这样我就可以将一些数据放入一个节点中。我记得我在学习 C 的时候花了一点时间来理解整个 struct typedef 概念。所以,不要放弃。

          #include <stdio.h>
          #include <stdlib.h>
          
          struct list {
              int data;
              struct list *next;
          };
          
          typedef struct list list_t;
          
          int main()
          {
              list_t *start, *cur;
              int i;
          
              start = (list_t *) malloc(sizeof(list_t));
          
              if (NULL != start)
              {
                  cur = start; /* Preserve list head, and assign to cur for list trarversal. */
                  printf("\nEnter the data : ");
                  scanf("%d", &i);
                  cur->data = i;
                  cur->next = NULL;
                  cur = start;
                  while(cur != NULL)
                  {
                      printf("%d ", cur->data);
                      cur = cur->next;
                  }
              }
              else
              {
                  printf("Malloc failed. Program ending.");
              }
          
              return 0;
          }
          

          【讨论】:

            猜你喜欢
            • 2012-05-19
            • 2014-04-22
            • 2012-07-13
            • 2013-05-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多