数组和指针的区别

C++/C程序中,指针和数组在不少地方可以相互替换着用,让人产生一种错觉,以为两者是等价的。

    数组要么在静态存储区被创建(如全局数组),要么在栈上被创建。数组名对应着(而不是指向)一块内存,其地址与容量在生命期内保持不变,只有数组的内容可以改变。

指针可以随时指向任意类型的内存块,它的特征是“可变”,所以我们常用指针来操作动态内存。指针远比数组灵活,但也更危险。

下面以字符串为例比较指针与数组的特性。

1 修改内容

       示例1中,字符数组a的容量是6个字符,其内容为hello\0。a的内容可以改变,如a[0]= ‘X’。指针p指向常量字符串“world”(位于静态存储区,内容为world\0),常量字符串的内容是不可以被修改的。从语法上看,编译器并不觉得语句p[0]= ‘X’有什么不妥,但是该语句企图修改常量字符串的内容而导致运行错误。

char a[] = “hello”;

a[0] = ‘X’;

cout << a << endl;

char *p = “world”;     // 注意p指向常量字符串

p[0] = ‘X’;             // 编译器不能发现该错误

cout << p << endl;

示例1 修改数组和指针的内容

2 内容复制与比较

    不能对数组名进行直接复制与比较。示例2中,若想把数组a的内容复制给数组b,不能用语句 b = a ,否则将产生编译错误。应该用标准库函数strcpy进行复制。同理,比较b和a的内容是否相同,不能用if(b==a) 来判断,应该用标准库函数strcmp进行比较。

    语句p = a 并不能把a的内容复制指针p,而是把a的地址赋给了p。要想复制a的内容,可以先用库函数malloc为p申请一块容量为strlen(a)+1个字符的内存,再用strcpy进行字符串复制。同理,语句if(p==a) 比较的不是内容而是地址,应该用库函数strcmp来比较。

    // 数组…

    char a[] = "hello";

    char b[10];

    strcpy(b, a);           // 不能用   b = a;

    if(strcmp(b, a) == 0)   // 不能用  if (b == a)

    // 指针…

    int len = strlen(a);

    char *p = (char *)malloc(sizeof(char)*(len+1));

    strcpy(p,a);            // 不要用 p = a;

    if(strcmp(p, a) == 0)   // 不要用 if (p == a)

示例2 数组和指针的内容复制与比较

3 计算内存容量

    用运算符sizeof可以计算出数组的容量(字节数)。示例3(a)中,sizeof(a)的值是12(注意别忘了’\0’)。指针p指向a,但是sizeof(p)的值却是4。这是因为sizeof(p)得到的是一个指针变量的字节数,相当于sizeof(char*),而不是p所指的内存容量。C++/C语言没有办法知道指针所指的内存容量,除非在申请内存时记住它。

注意当数组作为函数的参数进行传递时,该数组自动退化为同类型的指针。示例3(b)中,不论数组a的容量是多少,sizeof(a)始终等于sizeof(char *)。

    char a[] = "hello world";

    char *p  = a;

    cout<< sizeof(a) << endl;   // 12字节

    cout<< sizeof(p) << endl;   // 4字节

示例3(a) 计算数组和指针的内存容量

 

    void Func(char a[100])

    {

        cout<< sizeof(a) << endl;   // 4字节而不是100字节

}

示例3(b) 数组退化为指针

---------------------------------

指针和数组之字符串的区别

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. }
posted @ 2013-08-21 15:29  功夫 熊猫  阅读(550)  评论(2编辑  收藏  举报