重温C语言::1::(2)string之自由

1424阅读 0评论2011-07-13 onezeroone
分类:C/C++

1:概述
     发现char* 有几个有趣但平时没注意的特性。

2:例
  1. #include <stdio.h>

  2. static void
  3. tst_str_1(void)
  4. {
  5.     printf("hello" "world\n");
  6.     printf("hello"
  7.         "world\n");
  8. //    printf("hello", "world");
  9. }

  10. static void
  11. tst_str_2(void)
  12. {
  13.     char *as[] = {"hello",
  14.         "world"
  15.         "AAAAA",};

  16.     printf("%s ---- %s\n", as[0], as[1]);
  17. }

  18. static void
  19. initStr(char *s)
  20. {
  21.     static char sep = ' ';    
  22.     printf("%c%s", sep, s);
  23.     sep = ',';
  24. }

  25. static void
  26. tst_str_3(void)
  27. {
  28.     int i;
  29.     for (i = 0; i < 3; i++)    
  30.         initStr("Hello");
  31.     printf("\n");
  32. }

  33. int
  34. main(int argc, char **argv)
  35. {
  36.     tst_str_1();    
  37.     tst_str_2();    
  38.     tst_str_3();    

  39.     return 0;
  40. }

3:结果
  1. xdzh@xdzh-laptop:/media/home/work/c$ gcc -Wall -o tst-str tst-str.c
  2. xdzh@xdzh-laptop:/media/home/work/c$ ./tst-str
  3. helloworld
  4. helloworld
  5. hello ---- worldAAAAA
  6.  Hello,Hello,Hello



上一篇:重温C语言::1::(1)switch之秘密
下一篇:重温C语言::1::(3)符号之乱