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

函数声明在使用之后:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include<stdio.h>  
int main()  
{  
   foo(); // foo() is called before its declaration/definition  
}   
   
int foo()  
{  
   printf("Hello");  
   return 0;   
}

普通指针指向常量变量

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#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;  
}

空指针赋值给其他指针

1
2
3
4
5
6
7
#include
int main()
{
void vptr;
int iptr = vptr; // In C++, it must be replaced with int iptr = (int )vptr;
return 0;
}