有些C程序用C++编译器会报错,有如下几种情况:
1. 函数声明在使用之后:
#include<stdio.h>
int main()
{
foo(); // foo() is called before its declaration/definition
}
int foo()
{
printf("Hello");
return 0;
}
#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;
}
#include <stdio.h>
int main()
{
void *vptr;
int *iptr = vptr; // In C++, it must be replaced with int *iptr = (int *)vptr;
return 0;
}