High-quality C++/C Programming Guide-7th Chapter-Memory Management (2)

Source: Internet
Author: User
Tags array arrays comparison constant modify strcmp strlen

7.3.1 Modify Content

In the example 7-3-1, the capacity of the character array A is 6 characters, and its content is hello\0. The contents of a can be changed, such as a[0]= ' X '. The pointer p points to the constant string "world" (in the static store, the content is world\0), and the contents of the constant string cannot be modified. Syntactically, the compiler does not find anything wrong with the statement p[0]= ' X ', but the statement attempts to modify the contents of the constant string and cause a run 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 the error

cout << p << Endl;


Example 7-3-1 modifying the contents of arrays and pointers

7.3.2 Content Replication and comparison

You cannot copy and compare the array names directly. In the example 7-3-2, if you want to copy the contents of array A to array B, you cannot use statement B = A, or you will generate a compilation error. The standard library function strcpy should be used for replication. Similarly, the comparison between B and A is the same, not using if (b==a) to judge, should be compared with the standard library function strcmp.

The statement p = A does not copy the contents of a to the pointer p, but instead assigns the address of A to P. To copy the content of a, you can use the library function malloc for p to apply a strlen (a) + 1 characters of memory, and then use strcpy for string copying. Similarly, the statement if (P==a) compares not the content but the address, should use the library function strcmp to compare.

Array...

Char a[] = "Hello";

Char b[10];

strcpy (b, a); Can't use B = A;

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

...

Pointer...

int len = strlen (a);

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

strcpy (P,a); Do not use P = A;

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

...

Sample 7-3-2 and comparison of the contents of arrays and pointers

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.