苦苦挣扎:C中的指针

时间:2012-03-05 19:38:25

标签: c pointers

我已经在java中编程了一年多了,但我正在慢慢地教我自己C / objectiveC,同时在本书中学习:Cocoa和Objective C - Up and Running。我还在阅读介绍性章节,熟悉C语法与java的语法差异,并且遇到了关于动态内存的部分,特别是关于指针的部分。它提供的示例如下:

#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的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

如果有人可以解释为什么会出现这些错误,我会非常感激,我不明白为什么他们应该从书中作为一个例子。

感谢。

4 个答案:

答案 0 :(得分:3)

该计划并不好。您至少需要一个main()功能。添加:

int main(void)
{

#include行之后,添加:

  return 0;
}

在最后,它将编译。

答案 1 :(得分: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;
}

答案 2 :(得分:0)

您需要定义main函数:

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

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

答案 3 :(得分: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 );
}