Differences between arrays and pointers

Source: Internet
Author: User

In C ++/C Programs, pointers and arrays can be replaced with each other in many places, which leads to the illusion that the two are equivalent.

An array is either created in a static storage area (such as a global array) or on a stack. The array name corresponds to (rather than pointing to) a piece of memory, and its address and capacity remain unchanged during the lifetime, only the content of the array can be changed.

A pointer can point to any type of memory block at any time, and its feature is "variable". Therefore, we often use pointers to operate dynamic memory. Pointers are far more flexible than arrays, but they are more dangerous.

The following uses a string as an example to compare the features of pointers and arrays.

1Modify content

In example 1, the size of character array a is 6 characters, and its content is hello \ 0. The content of a can be changed, for example, a [0] = 'x '. The pointer p points to the constant string "world" (in the static storage area with the content of world \ 0). The content of the constant string cannot be modified. In terms of syntax, the compiler does not think that the statement p [0] = 'X' is inappropriate, but this statement attempts to modify the content of the constant string and causes a running error.

Char a [] = "hello ";

A [0] = 'X ';

Cout <a <endl;

Char * p = "world"; // note that p points to a constant string

P [0] = 'X'; // the compiler cannot find this error.

Cout <p <endl;

Example 1 modify the array and pointer content

2ContentCopy and Comparison

The array name cannot be directly copied or compared. In Example 2, if you want to copy the content of array a to array B, you cannot use statement B = a. Otherwise, a compilation error will occur. Use the standard library function strcpy for replication. Similarly, if the content of B and a is the same, it cannot be determined by if (B = a). The standard library function strcmp should be used for comparison.

Statement p = a does not copy the content of a, but assigns the address of a to p. To copy the content of a, use the library function malloc as p to apply for a memory with a capacity of strlen (a) + 1 characters, and then use strcpy to copy strings. Similarly, the statement if (p = a) compares not the content but the address, and should be compared using the database function strcmp.

// Array...

Char a [] = "hello ";

Char B [10];

Strcpy (B, a); // B = a cannot be used;

If (strcmp (B, a) = 0) // if (B = a) cannot be used)

...

// Pointer...

Int len = strlen ();

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

Strcpy (p, a); // do not use p =;

If (strcmp (p, a) = 0) // do not use if (p =)

...

Example 2 copying and comparing the array and pointer content

3Computing memory capacity

The sizeof operator can be used to calculate the array capacity (number of bytes ). In Example 3 (a), the value of sizeof (a) is 12 (do not forget '\ 0 '). The pointer p points to a, but the value of sizeof (p) is 4. This is because sizeof (p) obtains the number of bytes of a pointer variable, which is equivalent to sizeof (char *) rather than the memory capacity referred to by p. C ++/C language cannot know the memory capacity referred to by the pointer unless you remember it when applying for memory.

Note: When an array is passed as a function parameter, the array will automatically degrade to a pointer of the same type.In Example 3 (B), sizeof (a) is always equal to sizeof (char *) regardless of the size of array *).

Char a [] = "hello world ";

Char * p =;

Cout <sizeof (a) <endl; // 12 bytes

Cout <sizeof (p) <endl; // 4 bytes

Example 3 (a) Calculate the memory capacity of arrays and pointers

 

Void Func (char a [1, 100])

{

Cout <sizeof (a) <endl; // 4 bytes instead of 100 bytes

}

Example 3 (B) the array degrades to a pointer

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

1. String macro # define CONST_STR "const str" the Macro will be replaced with the actual value during pre-compilation.
2. the array name corresponds to a piece of memory. Its address and capacity will not change during the lifecycle, and the content in the array can be changed.
3. the pointer points to a piece of memory. If it points to a String constant (RO), the content cannot be modified. If you have applied for a piece of memory and copied a String constant, you can modify the content.

    Stdioh
  1. # Include h
  2. # Include unistdh
  3. # Include stdlibh
  4. # Define CONST_STR
  5. Typedef void string_test_routine_t
  6. Void pointer_strcpy_macrovoid
  7. Char p
  8. P charmalloc100
  9. Strcpyp CONST_STR
  10. Printf
  11. Printf
  12. P0
  13. Printf
  14. Void pointer_strcpy_stringvoid
  15. Char p
  16. P charmalloc100
  17. Strcpyp
  18. Printf
  19. Printf
  20. P0
  21. Printf
  22. Void pointer_macro_stringvoid
  23. Char p CONST_STR
  24. Printf
  25. Printf
  26. P0
  27. Printf
  28. Void pointer_stringvoid
  29. Char p
  30. Printf
  31. Printf
  32. P0
  33. Printf
  34. Void array_macro_stringvoid
  35. Char a100 CONST_STR
  36. Printf
  37. Printf
  38. A0
  39. Printf
  40. Void array_stringvoid
  41. Char a100
  42. Printf
  43. Printf
  44. A0
  45. Printf
  46. Void array_strcpy_stringvoid
  47. Char a100
  48. Strcpya
  49. Printf
  50. Printf
  51. A0
  52. Printf
  53. Void array_strcpy_macrovoid
  54. Char a100
  55. Strcpya CONST_STR
  56. Printf
  57. Printf
  58. A0
  59. Printf
  60. Static string_test_routine_t func_base
  61. Array_strcpy_string
  62. Array_macro_string
  63. Array_string
  64. Array_strcpy_macro
  65. Pointer_macro_string
  66. Pointer_string
  67. Pointer_strcpy_macro
  68. Pointer_strcpy_string
  69. # Define NUM_FUNC sizeof func_base sizeoffunc_base0
  70. Void test_stringvoid
  71. Pid_t pid
  72. I 0
  73. String_test_routine_t func func_base
  74. I 0 I NUM_FUNC I
  75. Pid fork
  76. Pid 0
  77. Printf
  78. Pid 0
  79. Funci
  80. 0
  81. Sleep 1
  82. Main
  83. Test_string
  84. Return 0

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.