그래서 나는 C에서 튜토리얼을 따르고 있고 malloc 함수를 사용하고 그 함수가 제 컴파일러 (Visual Studio C++ 10.0)와 잘 어울리지 않는 것 때문에 구조체에 갇혀 있습니다.C의 Malloc과 구조체가 오류를 발생시키고 있습니다 (튜토리얼에 따라)?
#include <stdio.h>
#include <stdlib.h>
struct node {
int x;
struct node *next;
};
int main()
{
/* This won't change, or we would lose the list in memory */
struct node *root;
/* This will point to each node as it traverses the list */
struct node *conductor;
root = malloc(sizeof(struct node));
root->next = 0;
root->x = 12;
conductor = root;
if (conductor != 0) {
while (conductor->next != 0)
{
conductor = conductor->next;
}
}
/* Creates a node at the end of the list */
conductor->next = malloc(sizeof(struct node));
conductor = conductor->next;
if (conductor == 0)
{
printf("Out of memory");
return 0;
}
/* initialize the new memory */
conductor->next = 0;
conductor->x = 42;
return 0;
}
이 malloc을 기능에 문제를주는 유지 : "값 그래서 정확히 지침을 따라 내가이 특정 코드를 제외 C를 컴파일 할 수 있습니다, 그것은 나에게 오류 (코드는 튜토리얼 웹 사이트에서 문자 그대로를) 제공 이것은 위에서 언급 한 오류를 해결하기 위해 듯하지만
root = malloc(sizeof(struct node));
등 : 유형의 무효 유형의 엔티티에 할당 할 수 없습니다 "노드 *"그래서 나는 즉, 모든 malloc에 함유 라인 (노드 *)를 주조 내가 그 일을하고 컴파일하려고 시도 할 때 새로운 오류가 발생했습니다 :
1>------ Build started: Project: TutorialTest, Configuration: Debug Win32 ------
1> TutorialTest.c
1>c:\users\ahmed\documents\visual studio 2010\projects\tutorialtest\tutorialtest\tutorialtest.c(16): error C2065: 'node' : undeclared identifier
1>c:\users\ahmed\documents\visual studio 2010\projects\tutorialtest\tutorialtest\tutorialtest.c(16): error C2059: syntax error : ')'
1>c:\users\ahmed\documents\visual studio 2010\projects\tutorialtest\tutorialtest\tutorialtest.c(27): error C2065: 'node' : undeclared identifier
1>c:\users\ahmed\documents\visual studio 2010\projects\tutorialtest\tutorialtest\tutorialtest.c(27): error C2059: syntax error : ')'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
그래, 30 분 동안 이것을 알아 내려고하자 (완전한 C 초보자로서), 나는 해결책을 찾지 못했습니다. 이 오류를 어떻게 해결할 수 있습니까? 나는 그것이 컴파일러 이슈라고 생각하기 시작했다. 그러나 그것이 필연적이지 않다면 컴파일러를 바꾸고 싶지 않다.
대단히 감사합니다. 최대한 빨리 답변을 선택하겠습니다 :) – ZimZim
파일 이름이'.c'로 끝나면 VC10은이 파일을 C로 컴파일합니다. 그러나 C로 작성하려면, 다른 컴파일러 사용을 진지하게 고려할 것입니다. Microsoft의 C 지원은 매우 미미한 수준입니다. –