码迷,mamicode.com
首页 > 编程语言 > 详细

9.12 C语言知识大复习 gbk2utf8

时间:2014-09-15 01:04:07      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   2014   

字符串常量的写法:
char *s = "hello world";
最好改写成   const  char *s = "hello world";
因为修改其内容也会出错。


函数返回地址的区别:

函数返回地址,除了堆地址和字符串常量地址有意义。其他都无意义。
  1. #include <stdio.h>
  2. const char *getstr()
  3. {
  4. const char *s = "hello world"; //返回一个常量字符串地址是有效的
  5. return s;
  6. }
  7. int main()
  8. {
  9. printf("%s\n",getstr());
  10. return 0;
  11. }

错误:
  1. #include <stdio.h>
  2. const char *getstr()
  3. {
  4. char s[100] = "hello world"; //返回一个栈上的地址是无意义的,因为函数结束即释放
  5. return s;
  6. }
  7. int main()
  8. {
  9. printf("%s\n",getstr());
  10. return 0;
  11. }


++运算符你真的了解了吗
  1. #include <stdio.h>
  2. int main()
  3. {
  4. int i = 9;
  5. int a = ++i++; //先计算i++,得到的值是没有内存存放的,无法作为左值。无法再++i
  6. printf("%d\n",a);
  7. }
编译时就直接出错。
bubuko.com,布布扣

bubuko.com,布布扣




gbk2utf8
  1. int gbk2utf8(char *src, size_t *srclen, char *dest, size_t *destlen)
  2. {
  3. iconv_t cd = iconv_open("UTF8", "GBK"); //源字符串为GBK,目标UTF8
  4. if (cd == (iconv_t) - 1)
  5. {
  6. printf("open iconv error %s\n", strerror(errno));
  7. return -1;
  8. }
  9. size_t rc = iconv(cd, &src, srclen, &dest, destlen); //将src字符串转化为目标dest
  10. if (rc == (size_t) - 1)
  11. {
  12. printf("iconv error %s\n", strerror(errno));
  13. return -1;
  14. }
  15. iconv_close(cd);
  16. return 0;
  17. }





9.12 C语言知识大复习 gbk2utf8

标签:des   style   blog   http   color   io   os   ar   2014   

原文地址:http://www.cnblogs.com/l6241425/p/3972030.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!