そのため、Cのチュートリアルに従っていますが、構造体はmalloc関数を使用しており、その関数は私のコンパイラ(Visual Studio C ++ 10.0)ではうまく機能しないようです。したがって、私は指示に正確に従い、Cをコンパイルできますが、この特定のコードではエラーが発生します(チュートリアルのWebサイトから文字通り取得したコード)。
#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関数は問題を出し続けました:「タイプvoidの値をタイプ "node *"のエンティティに割り当てることができないので、私はすべてのmallocを含む行に(ノード*)をキャストします。
root = malloc( sizeof(struct node) );
など。これで上記のエラーは解決したようですが、それを実行してコンパイルしようとすると、新しいエラーが発生しました。
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 ==========
そうですね、(完全なC初心者として)これを30分間理解しようとした後、私は解決策を思い付くことができませんでした。このエラーを解決するにはどうすればよいですか?コンパイラの問題だと思い始めていますが、必要がなければコンパイラを変えたくありません。