C++编译不了的C程序

来源:互联网 发布:x80锐途凯立德导航端口 编辑:程序博客网 时间:2024/06/10 23:16

有些C程序用C++编译器会报错,有如下几种情况:

1.  函数声明在使用之后:

#include<stdio.h>int main(){   foo(); // foo() is called before its declaration/definition}  int foo(){   printf("Hello");   return 0; }


2. 普通指针指向常量变量

#include <stdio.h> int main(void){    int const j = 20;     /* The below assignment is invalid in C++, results in error       In C, the compiler *may* throw a warning, but casting is       implicitly allowed */    int *ptr = &j;  // A normal pointer points to const     printf("*ptr: %d\n", *ptr);     return 0;}


3. 空指针复制给其他指针

#include <stdio.h>int main(){    void *vptr;    int *iptr = vptr; // In C++, it must be replaced with int *iptr = (int *)vptr;     return 0;}


 








0 0
原创粉丝点击