2012-04-26 70 views
1

我有以下代码。 我收到错误为'list' undeclared (first use in this function)'list'未申报(首次在此功能中使用)

#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; 
} 
+2

您没有张贴截图... – ThiefMaster 2012-04-26 12:04:15

+0

@ThiefMaster比邮寄没有(没有代码,没有错误日志)破越好.. – Krishnabhadra 2012-04-26 12:05:13

+1

评论:1.不投的malloc'的返回值( )'; 2.不要使用'sizeof struct'作为它的参数,而是使用'sizeof(* start)'; 3.当你从不使用它时,为什么'typedef struct list * head'? – 2012-04-26 12:08:11

回答

3

您使用,而不是变量名start类型list请帮我。正确代码:

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

非常感谢...雅我犯了一个错误 – 2012-04-26 12:07:08

+0

这只是第二个错误修正。 – 2012-04-26 12:17:30

3

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)); 

这样的(指针)类型的start,当你以后更改的start类型是有用的,你会自动malloc足够的字节 - 的malloc通话不会改变一点。

0
start = (struct list *) malloc ... 

您错过了struct在铸造。根据Anthales指出,这根本不是必需品。

start = malloc ... 
+2

为了上帝的缘故,不要使用返回类型的malloc!停止这个C++的疯狂! – Anthales 2012-04-26 12:09:00

1

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

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

start = malloc(sizeof(struct list)); 

但是,你的代码比这更麻烦。我可以通过询问我自己的问题来回答你的问题:在你的脑海中,是一个类型还是一个对象list

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

1
#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; 
} 

可以定义

1

你的变量命名为类型(名单*)«开始»,你把它称为«列表»。

0

您遇到的一个问题是您在创建typedef struct list *start;后重新使用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; 
} 
相关问题