2012-04-20 3 views
0

이것은 쉬운 링크 목록 문제라고 생각하지만 main() 함수에 input_info()를 추가하면 MSVC cl 컴파일러에서 다음과 같은 오류 메시지를 표시합니다.이상한 오류 메시지가 C에서 구조화 된 노드와 연결된 목록과 연결된

syntax error : missing ';' before 'type' 
error C2065: 'first_ptr' : undeclared identifier 
warning C4047: 'function' : 'struct linked_list *' differs in levels of indirection from 'int ' 
warning C4024: 'print_list' : different types for formal and actual parameter 1 

나는 컴파일러가 다음과 같은 오류를 표시하는 이유를 모르겠다 ... 왜 MSVC 6.0 컴파일러가 나에게 이처럼 오류 메시지를주는 지 알게 해줘.

/* 
* 
*  this program is a simple link list which will have add(), 
* remove(), find(), and tihs link list will contains the student 
* information. 
*   
*/ 

#include <stdlib.h> 
#include <stdio.h> 

//this ppl structure student holds student info 
typedef struct 
{ 

    char *name; 
    int height; 
    int weight; 

}ppl; 

//this structure is the link list structure 
typedef struct linked_list 
{ 
    ppl data; 
    struct linked_list *next_ptr; 
} linked_list; 

//this function will print the link list in a reversed order 
void print_list(linked_list *a) 
{ 

    linked_list *llp=a; 
    while (llp!=NULL) 
    { 
    printf("name: %s, height: %d, weight: %d \n",llp->data.name,llp->data.height,llp->data.weight); 
     llp=llp->next_ptr; 

    } 
} 

//this function will add ppl info to the link list 
void add_list(ppl a, linked_list **first_ptr) 
{ 
    //new node ptr 
    linked_list *new_node_ptr; 
    //create a structure for the item 
    new_node_ptr=malloc(sizeof(linked_list)); 

    //store the item in the new element 
    new_node_ptr->data=a; 
    //make the first element of the list point to the new element 
    new_node_ptr->next_ptr=*first_ptr; 

    //the new lement is now the first element 
    *first_ptr=new_node_ptr; 
} 

void input_info(void) 
{ 
    printf("Please input the student info!\n"); 
} 


int main() 
{ 
    ppl a={"Bla",125,11}; 

    input_info(); 
    //first node ptr 
    struct linked_list *first_ptr=NULL; 

    //add_list(a, &first_ptr); 
    printf("name: %s, height: %d, weight: %d \n",a.name,a.height,a.weight); 

    //print link list 
    print_list(first_ptr); 
    return 0; 
} 
+0

일반적으로 'internal'구조체 이름과 typedef 이름에 다른 식별자를 지정합니다. typedef에서'struct linked_list'를 다른 것으로 변경하면 도움이됩니까? (모든 오류는'linked_list' 만 이해하지 못하는 것과 관련이 있습니다.) 오류를보고있는 행을 확인하면 일반적으로 도움이됩니다. – Rup

+0

사실 MSVC IIRC에서 허용해서는 안되는'input_info()'다음에'struct linked_list * first_ptr'을 선언했습니다. C++가 아닌 C로 컴파일하고 있습니까? – Rup

+0

어림짐작으로서, 유형이 비슷하지만 사용 방법과 방식이 다른 변수에 대해서는 정확히 같은 이름을 사용하지 마십시오.'linked_list 유형의 변수에'first_ptr' 이름을 사용하지 마십시오 **'또는'struct linked_list *', 등등. 너 자신을 혼란스럽게 할 뿐이다. 그들에게 더 정확한 이름을주십시오. –

답변

1

에, 당신은 너무

input_info(); 
struct linked_list *first_ptr=NULL; 

가 작동하지 않습니다, 선언과 코드를 혼합 할 수 없습니다 , since after input_info(); 컴파일러는 형식을 볼 수 없으므로이 형식을 사용할 수 없습니다. 단순히 다음과 같이 변경하십시오 :

struct linked_list *first_ptr=NULL; 
input_info(); 

모든 것이 작동합니다.

+0

대단히 고마워요 ... 이런 종류의 규칙을 염두에 두겠습니다. – shanwu

1

변화는 엄격하게 C89입니다 MSVC에 comiling하고 있기 때문에

ppl a={"Bla",125,11}; 
//first node ptr 
struct linked_list *first_ptr=NULL; 
input_info(); 
+0

변수 정의는 블록의 시작 부분에 요약되어 있습니다 (오래된 c를 사용하는 경우). – BLUEPIXY

+0

대단히 감사합니다. 그것은 효과가있다. – shanwu

관련 문제