C 程序(malloc)未在 ubuntu 11.04 中编译

标签 c linux ubuntu-11.04

我已经通过这个 sudo apt-get install build-essential 命令安装了 gcc 编译器

我的程序代码是

 #include<stdio.h>

 main()
   {
      int *b;

      b = (int*)malloc(10*sizeof(int));  

      printf("b=%u\n\n",b);
      printf("b+1=%u\n\n",(b+1));
      printf("b+2=%u\n\n",(b+2));

      b[2]=4;
      printf("*(b+2)=%d\n\n",*(b+2));

  }

当我尝试从 cc -c program.c 命令编译这个程序时 然后我得到一些错误

enter image description here

最佳答案

你错过了 #include <stdlib.h> (对于 malloc ),格式字符串错误。使用 %p打印指针。

此外,您不需要 ( and probably shouldn't ) 转换 malloc 的返回值(在 C 中)。

以及 main 的正确签名没有参数是:

int main(void)

更正后的代码:

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

int main()
{
    int *b;

    b = (int*)malloc(10*sizeof(int));

    printf("b=%p\n\n",  (void*) b);
    printf("b+1=%p\n\n",(void*) (b+1));
    printf("b+2=%p\n\n",(void*) (b+2));

    b[2]=4;
    printf("*(b+2)=%d\n\n",*(b+2));

    return 0;
}

关于C 程序(malloc)未在 ubuntu 11.04 中编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8872192/

相关文章:

apache - 安装Pig包

python - Ubuntu Unity 下 PyGTK 菜单不显示?

c++ - 如何使用断言函数

c - SHA256 函数更改输入变量

php - 如何在 Debian Linux 中允许用户 www-data 访问 i2c?

c - 管道,当父关闭 fd[1] 时,子将从 fd[0] 中得到什么?

php - 转义 SED 命令

node.js - now.js : "Object has no method" error message when trying to start server. js

c - 我的代码中是否存在内存泄漏? (使用指针存储字符串)

c++ - 长双字面量的 C++ 后缀是什么?