Chinaunix首页 | 论坛 | 博客
  • 博客访问: 333188
  • 博文数量: 201
  • 博客积分: 305
  • 博客等级: 二等列兵
  • 技术积分: 500
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-30 16:58
文章分类

全部博文(201)

文章存档

2015年(5)

2014年(12)

2013年(173)

2012年(11)

我的朋友

分类: C/C++

2013-03-18 13:44:50

1. 字符串宏
#define CONST_STR "const str"
宏在预编译的时候会替换成实际的值

2.数组
数组名对应一块内存,在生命周期内其地址和容量不会改变,数组里面的内容可以变。

3.指针
指针指向一块内存,如果指向字符串常量(RO),则不能修改内容。
如果申请了一块内存,复制字符串常量,则可以修改内容


  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>


  5. #define CONST_STR "const string"
  6. typedef void (*string_test_routine_t)();

  7. void pointer_strcpy_macro(void)
  8. {
  9.     char *p;
  10.     p = (char*)malloc(100);
  11.     strcpy(p, CONST_STR);
  12.     printf("char *p;\np = (char *)malloc(100);\nstrcpy(p, CONST_STR);\np[0] = 'a';\n");
  13.     printf("\n");
  14.     p[0] = 'a';
  15.     printf("--> ok\n\n\n");
  16. }
  17. void pointer_strcpy_string(void)
  18. {
  19.     char *p;
  20.     p = (char*)malloc(100);
  21.     strcpy(p, "const string");
  22.     printf("char *p;\np = (char *)malloc(100);\nstrcpy(p, \"const string\");\np[0] = 'a';\n");
  23.     printf("\n");
  24.     p[0] = 'a';
  25.     printf("--> ok\n\n\n");
  26. }

  27. void pointer_macro_string(void)
  28. {
  29.     char *p = CONST_STR;
  30.     printf("char *p = CONST_STR;\np[0] = 'a'; \n");
  31.     printf("\n");
  32.     p[0] = 'a';
  33.     printf("--> ok\n\n\n");
  34. }

  35. void pointer_string(void)
  36. {
  37.     char *p = "const string";
  38.     printf("char *p = \"const string\";\np[0] = 'a'; \n");
  39.     printf("\n");
  40.     p[0] = 'a';
  41.     printf("--> ok\n\n\n");
  42. }


  43. void array_macro_string(void)
  44. {
  45.     char a[100] = CONST_STR;
  46.     printf("char a[100] = CONST_STR;\na[0] = 'a'; \n");
  47.     printf("\n");
  48.     a[0] = 'a';
  49.     printf("--> ok\n\n\n");
  50. }


  51. void array_string(void)
  52. {
  53.     char a[100] = "const string";
  54.     printf("char a[100] = \"const string\";\na[0] = 'a'; \n");
  55.     printf("\n");
  56.     a[0] = 'a';
  57.     printf("--> ok\n\n\n");
  58. }
  59. void array_strcpy_string(void)
  60. {
  61.     char a[100];
  62.     strcpy(a, "const string");
  63.     printf("char a[100];\nstrcpy(a, \"const string\");\na[0] = 'a';\n");
  64.     printf("\n");
  65.     a[0] = 'a';
  66.     printf("--> ok\n\n\n");
  67. }

  68. void array_strcpy_macro(void)
  69. {
  70.     char a[100];
  71.     strcpy(a, CONST_STR);
  72.     printf("char a[100];\nstrcpy(a, CONST_STR);\na[0] = 'a';\n");
  73.     printf("\n");
  74.     a[0] = 'a';
  75.     printf("--> ok\n\n\n");
  76. }

  77. static string_test_routine_t func_base[] = {
  78.     array_strcpy_string,
  79.     array_macro_string,
  80.     array_string,
  81.     array_strcpy_macro,
  82.     pointer_macro_string,
  83.     pointer_string,
  84.     pointer_strcpy_macro,
  85.     pointer_strcpy_string
  86. };

  87. #define NUM_FUNC (sizeof (func_base) / sizeof(func_base[0]))

  88. void test_string(void)
  89. {
  90.     pid_t pid;
  91.     int i = 0;
  92.     string_test_routine_t * func = func_base;

  93.     for ( i = 0; i < NUM_FUNC; i++ )
  94.     {
  95.         pid = fork();
  96.         if ( pid < 0 )
  97.         {
  98.             printf("fork() err\n");
  99.         }
  100.         else if ( pid == 0 )
  101.         {
  102.             (func[i])();
  103.             exit(0);
  104.         }
  105.         else
  106.         {
  107.             sleep( 1 );
  108.         }
  109.     }
  110. }

  111. int main()
  112. {
  113.     test_string();

  114.     return 0;
  115. }

阅读(250) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~