Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1683095
  • 博文数量: 347
  • 博客积分: 9328
  • 博客等级: 中将
  • 技术积分: 2680
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-29 23:45
文章分类

全部博文(347)

文章存档

2016年(1)

2013年(4)

2012年(207)

2011年(85)

2010年(50)

分类: C/C++

2012-12-06 22:08:17

原文:http://blog.csdn.net/w343051232/article/details/7074788

由于cpp编译下列代码报错

  1. #include   
  2. #include   
  3.   
  4. int main()  
  5. {  
  6.   
  7.    wchar_t wstr[] = L"Hello!";  
  8.    int wlen = strlen(wstr);  
  9.    wprintf(L"%ls",wstr);  
  10.    int len;  
  11.   
  12.       
  13.   
  14.     return 0;  
  15. }  

error C2664: 'strlen' : cannot convert parameter 1 from 'unsigned short [7]' to 'const char *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

于是将后缀名改成.c

却出现另外一个诡异的编译错误:error C2143: syntax error : missing ';' before 'type' 并且指向 int len;这行

将代码做如下修改却诡异般的编译过了

  1. #include   
  2. #include   
  3.   
  4. int main()  
  5. {  
  6.   
  7.    wchar_t wstr[] = L"Hello!";  
  8.    int len;  
  9.    int wlen = strlen(wstr);  
  10.    wprintf(L"%ls",wstr);  
  11.   
  12.       
  13.   
  14.     return 0;  
  15. }  

不知道什么原因!!找了找资料。。。。原来It is a rule! 在纯C中,在一个代码块中变量声明必须在最前面
阅读(1613) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~