2012-03-05 89 views
2

指針我一直在Java編程了一年多,但現在正在慢慢自學C /的ObjectiveC,同時在大學從書中學習:可可和目標C - 啓動和運行。我仍然閱讀介紹性章節,熟悉C語言與java的語法差異,並且已經遇到了關於動態內存的一節,特別是指針。它提供的示例是這樣的:與編圖掙扎:用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); 

我理解代碼 - 創建的整數指針,分配的存儲器10塊,創建一個第二指針在數字的第一動態存儲器塊指向,設置第一塊到100,遞增到第二個塊並將其設置爲200,然後使用free()釋放內存。

然而,當我嘗試編譯我收到了一系列的錯誤。代碼保存在一個名爲dynamic.c的文件夾中,名爲Dynamic.c。

這裏是在終端發生什麼打印:

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

我想你錯過了'的main()'方法。 – Mysticial 2012-03-05 19:40:16

+0

你說_代碼保存在c class_中。那麼,什麼是C類?請顯示整個Dynamic.c – sidyll 2012-03-05 19:43:05

回答

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 2012-03-05 20:30:40

3

那個節目也是白搭。您至少需要一個main()函數。地址:

int main(void) 
{ 

#include線後,並添加:

return 0; 
} 

在最後,它將編譯。

+0

你只是把建議扔到空中。發佈的代碼當然不是他/她正在編譯的整個文件。 – sidyll 2012-03-05 19:44:39

+0

認真嗎?添加這些行*會使其編譯。如果OP想要更好的答案,他必須提出一個更好的問題。 – 2012-03-05 19:45:12

+0

我知道。這只是一個建議:-)不要浪費你的時間與不確定的事情。這個問題還不清楚,所以請留下評論,要求改進,而不是寫出完整的答案。 – sidyll 2012-03-05 19:48:19

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