Comparison of pointers to arrays

Source: Internet
Author: User

In c++/c programs, pointers and arrays can be substituted for each other in many places, giving people an illusion to is equivalent to the two.

Arrays are either created in a static store (such as a global array) or created on a stack. The array name corresponds to (instead of pointing to) a piece of memory whose address and capacity remain constant over the lifetime, and only the contents of the array can be changed. Pointers can point to any type of block of memory at any time, and its characteristics are "mutable", so we use pointers to manipulate dynamic memory. Pointers are far more flexible than arrays, but they are also more dangerous.

The following example compares the properties of pointers and arrays with a string :

1 . Modify Content

shownin Example 1 , the character array a has a capacity of 6 characters and its contents are hello\0 . The contents of a can be changed change, such asa[0]='X'. PointersPpoint to the constant string " World(located in the static storage area, the content isworld\0),The contents of a constant string cannot be modified. Syntactically, the compiler does not feel that the statementp[0]='X' What'sis inappropriate, but the statement attempts to modify the contents of a constant string to cause a run error.

Example 1:

Char a[] = "Hello" ;

A[0] = ' X ' ;

cout<<a<<endl;

Char *p = "World" ;  //Note p points to the constant string

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

cout<<p<<endl;

2 . content replication and comparison

2 a B&NBSP; , cannot use the statement B&NBSP;=&NBSP;A&NBSP; ST&NBSP;RCPY&NBSP; For replication.

Similarly, comparing the contents of b and a is the same, cannot be judged by I f (b==a) , should use the standard library function St rcmp for comparison. The statement p = A does not copy the contents of a pointer p , but assigns the address of A to p . To copy a numberGroup, you can use the library function Mal loc for P to apply a capacity of St R L en (a) +1 characters of memory, and then use St Rcpy for string copying. Similarly, the statement I f (p==a) compares not the content but the address, should use the library

function St RCMP to compare.

Example 2:

Array

Char a[] = "Hello" ;

Char b[10];

strcpy (b,a); //cannot be used with B = A;

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

Pointer

int len = strlen (a);

char Span style= "font-family: ' The new song Body '; Font-size:13px;background:rgb (255,255,255);" > *p =  ( char  *) malloc ( sizeof Span style= "font-family: ' The new song Body '; Font-size:13px;background:rgb (255,255,255);" > ( char

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

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

3 . Calculate memory Capacity

Use the operator sizeof to calculate the capacity (in bytes) of the array. shownCases3in whichsi zeof (a)the value is A(Be careful not to forget '\ 0'). PointersPPointingabutsi zeof (p)is the value of4. This is becausesi zeof (p)Gets the number of bytes of a pointer variable, which is equivalent tosi zeof (char *), but notPThe memory capacity referred to.

Note : When an array is passed as an argument to a function, the array is automatically degraded to a pointer of the same type. in Example 3 , regardless of the capacity ofarray A,sizeof (a) is always equal to sizeof (char *) .

Example 3:

Char a[] = "Hello World" ;

Char *p = A; //*p is a pointer variable

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

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

void Func (char a[+])

{

cout<< sizeof (a) <<endl; //4 bytes Instead of 100 bytes

}


This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1766693

Comparison of pointers to arrays

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.