Pointers vs. arrays (--selected from: C + + memory management Technology Insider)

Source: Internet
Author: User
Tags strcmp

Array:

Arrays are either created on a static store (such as a global array) or created on a stack. The array name represents

Contiguous memory, whose address and capacity will not change over the life cycle, but can only change its array contents.

Pointer:

A pointer is a pointer-type variable, and the variable is the first address of a memory that can be changed, so you can generally use pointers

To point to the dynamically opened memory.

The following is a string example to compare arrays and pointers :

1. Modify the Content

Char a[] = "Hello";
A[0] = ' X ';
cout << a << endl;//output is Xello
Char *p = "World"; Note P points to the constant string
P[0] = ' X '; The compiler cannot discover that the contents of the error//constant string cannot be modified.
cout << p << Endl;

2. Content replication and comparison

You cannot copy and compare directly to a group name. If you want to copy the contents of array A to array B, you cannot use statement B = A, otherwise
Generates a compilation error. The standard library function strcpy should be used for replication. Similarly, compare the contents of B and a are the same, not with if (b==a)
To judge, the standard library function strcmp should be used for comparison.
A is an array name, p is a pointer statement p = A does not copy the contents of a to pointer p, but assigns the address of A to P.

To Copy the contents of a, you can first use the library The function malloc for p applies a piece of memory with a capacity of strlen (a) + 1 characters, and then uses a strcpy

for string copying. Similarly, the statement if (p==a) comparison is not the content but the address, should be compared with the library function strcmp.

3. Calculate Memory capacity

Pointer p points to a, but sizeof (p) has a value of 4. This is because sizeof (p) Gets the number of bytes of a pointer variable, which is equivalent to
sizeof (char*), not the memory capacity referred to by P. The c++/c language has no way of knowing the amount of memory the pointer refers to, unless it is remembered in the application
when it is stored.
       char a[] = "Hello World";
       char *p = A;
       cout<< sizeof (a) << Endl;//12 bytes
& nbsp      cout<< sizeof (p) << Endl;//4 bytes, which represents the number of bytes that a character pointer occupies
        Note that when an array is passed as an argument to a function, the array is automatically degraded to a pointer of the same type. In the following example, sizeof (a) is always equal to sizeof (char *), regardless of the amount of
of the array A's capacity.
void Func (char a[100])
{
cout<< sizeof (a) << Endl;//4 bytes instead of 100 bytes
}

Pointers vs. arrays (--selected from: C + + memory management Technology Insider)

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.