2012-03-05 7 views
2

私はJavaで1年以上プログラミングしてきましたが、CocoaとObjective C - Up and Runningという本からuniで勉強しながら、C/objectiveCをゆっくりと教えています。私はまだC言語の構文上の違いをjavaに慣れ親しんでいる入門的な章を辿り、特にポインタに関する動的メモリのセクションを見てきました。 、整数のポインタを作成し、それをメモリの10個のブロックを割り当て、数字の最初の動的メモリブロックを指す第2のポインタを作成し、最初に設定 - 私はコードを理解コンパイルで苦労している:Cのポインタ

#include <stdio.h> 
#include <stdlib.h> 
int* numbers; 
numbers = malloc (sizeof(int) * 10); 

//create a second variable to always point at the 
//beginning of numbers memory block 
int* numbersStart; 
numbersStart = numbers; 

*numbers = 100; 
numbers++; 
*numbers = 200; 

//use the 'numbersStart' variable to free the memory instead 
free(numbersStart); 

:それが提供する例は、このですブロックを100に、インクリメントして2番目のブロックを200に設定し、free()を使用してメモリを解放します。

しかし、コンパイルしようとすると、一連のエラーが発生します。コードはDynamic.cという名前のcクラスに、dynamicというフォルダに保存されます。ここ

は、端末で発生するものの印刷です:

gcc Dynamic.c -o Dynamic 
    Dynamic.c:13: warning: data definition has no type or storage class 
    Dynamic.c:13: error: conflicting types for ‘numbers’ 
    Dynamic.c:12: error: previous declaration of ‘numbers’ was here 
    Dynamic.c:13: warning: initialization makes integer from pointer without a cast 
    Dynamic.c:13: error: initializer element is not constant 
    Dynamic.c:15: warning: data definition has no type or storage class 
    Dynamic.c:15: error: conflicting types for ‘numbersStart’ 
    Dynamic.c:14: error: previous declaration of ‘numbersStart’ was here 
    Dynamic.c:15: error: initializer element is not constant 
    Dynamic.c:16: warning: data definition has no type or storage class 
    Dynamic.c:16: warning: initialization makes pointer from integer without a cast 
    Dynamic.c:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘++’ token 
    Dynamic.c:18: warning: data definition has no type or storage class 
    Dynamic.c:18: error: redefinition of ‘numbers’ 
    Dynamic.c:16: error: previous definition of ‘numbers’ was here 
    Dynamic.c:18: warning: initialization makes pointer from integer without a cast 
    Dynamic.c:19: warning: data definition has no type or storage class 
    Dynamic.c:19: warning: parameter names (without types) in function declaration 
    Dynamic.c:19: error: conflicting types for ‘free’ 
    /usr/include/stdlib.h:160: error: previous declaration of ‘free’ was here 

これらのエラーは、私は非常に義務づけられる発生理由を誰かが説明できるならば、彼らはそのようの例であるべき理由を、私は見ていません本。

ありがとうございました。

+0

私はあなたが 'メイン()'メソッドが欠落していると思います。 – Mysticial

+0

あなたは言った_コードはcクラスに保存されました_。さて、cクラスは何ですか? Dynamic.c全体を表示してください – sidyll

答えて

1

main()機能で包み:

#import <stdio.h> 
#import <stdlib.h> 

int main() 
{ 
    int* numbers; 
    numbers = malloc (sizeof(int) * 10); 

    //create a second variable to always point at the 
    //beginning of numbers memory block 
    int* numbersStart; 
    numbersStart = numbers; 

    *numbers = 100; 
    numbers++; 
    *numbers = 200; 

    //use the 'numbersStart' variable to free the memory instead 
    free(numbersStart); 

    return 0; 
} 
+0

Duh !!!私はそれをメインに置くことを忘れていたとは信じられません。ありがとう、私はそれが非常に単純な何かであることがわかった束。 – schanq

3

このプログラムはいいです。少なくともmain()機能が必要です。右#include行の後

int main(void) 
{ 

、および追加:追加

return 0; 
} 

非常に終わり、それがコンパイルされます。

+0

あなたは空中にアドバイスを投げています。確かに投稿されたコードは、彼/彼女が編集しているファイル全体ではありません。 – sidyll

+0

真剣に?これらの行を追加すると*コンパイルされます。 OPがより良い答えを望むなら、彼はより良い質問をしなければならない。 –

+0

私は知っています。それは単なるアドバイスです:-)あなたの時間を無駄にしないでください。質問は不明なので、答え全体を書くのではなく、改善を求めるコメントを残しておきます。 – sidyll

0

あなたはmain関数を定義する必要があります。

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

int main(void) 
{ 
    int* numbers; 
    numbers = malloc (sizeof(int) * 10); 
    ... 
    free(numbersStart); 
    return EXIT_SUCCESS; 
} 
0
numbers = malloc (sizeof(int) * 10); 

これは声明で、あなたは機能とプットを使ってプログラムを整理C.

における関数の外側のステートメントを持つことができません関数本体のステートメントこれは正しいです:

// This defines a function named foo that takes no argument and returns no value 
void foo(void) 
{ 
    int* numbers; 
    numbers = malloc (sizeof(int) * 10); 

    //create a second variable to always point at the 
    //beginning of numbers memory block 
    int* numbersStart; 
    numbersStart = numbers; 

    *numbers = 100; 
    numbers++; 
    *numbers = 200; 

    //use the 'numbersStart' variable to free the memory instead 
    free(numbersStart); 
} 
関連する問題