Measure the test taker's knowledge about the char array, pointer, and strcpy functions in C ++.

Source: Internet
Author: User

First, declare that this is an excerpt. In order to obtain the page views, the original address is the original address.

1. When declaring a character array, the number in [] should be the number of characters in the array, including '/0'
For example, char p [5] = "dddd ";
The actual value is: 'd 'D '/0 '.
If char p [5] = "ddddd ";
An error occurred while compiling.
 
2. (1) when the character array is initialized, '/0' is automatically added'
For example, char p [5] = "dd ";
Actually: 'D' D'/0'/0'/0'/0'
Another example is char p [3] = "";
Actually: '/0'/0'/0'/0'
(2) If the character array is not declared, '/0' will not be automatically supplemented if it is not initialized'
For example, char p [3];
The content of the actual character array is unknown, because p is actually a pointer and does not know its direction.
(3) Similarly, when the character pointer is declared and is not initialized
Such as char * p;
(4) declare and initialize the pointer. Because of the "memory alignment" (implemented by the compiler ),
4 bits are added, and '/0' is automatically added'
For example, char * p = "";
It is actually: p [0] = 'A', p [1] = '/0', p [2] ='/0 ', p [3] = '/0 '.
If char * p = "";
It is actually: p [0] = '/0', p [1] ='/0', p [2] = '/0 ', p [3] = '/0 '.
 
 
3. The strlen (const char * p) function returns the number of characters in the character array, excluding '/0'
For example, char p [5] = "dddd ";
Char p2 [3] = "";
Int I = strlen (p );
Int j = strlen (p2 );
At this time, I = 4, j = 0
 
4. About strcpy Functions
Function implementation:
Char * strcpy (char * strDestination, const char * strSource)
{
Assert (strDestination & strSource );
Char * strD = strDestination;
While (* strDestination ++ = * strSource ++ )! = '/0 ')
NULL;
Return strD;
}
This function does not check whether subscripts are out of bounds. Therefore, pay attention to the following when using this function:
-> The first parameter passed to the function must be a character array or a character pointer to a character array.
-> And the array size of the first parameter should be greater than or equal to that of the second parameter.
(1) The following code first discusses the correct parameter passing:
Char a [] = "123"; char B [] = "123456"; // It can also be char * a = "123"; char * B = "123456 ";
Char p1 [4]; char p2 [9]; // The number in [] must be greater than or equal to the size of the second parameter character array
Char * p3; p3 = p1;
Char * p4; p4 = p2;
Strcpy (p1, a); strcpy (p2, B );
Strcpy (p3, a); strcpy (p4, B );
In the above Code, copying is correct.
(2) error code 1:
Char a [] = "123"; char * p1;
Char * p2 = "";
Strcpy (p1, a); // -------- 1
Strcpy (p2, B); // -------- 2
The above code can be compiled, but errors may occur in lines 1 and 2 during the runtime and the code is exited.
The reason is that p1 is not initialized and the point is not determined. overwriting the content indicated by p1 with "123" will cause an error,
Overwriting p2 may also result in an error caused by modifying the uncertain value originally pointed.
(3) Error Code 2:
Char a [] = "123456 ";
Char p [2];
Strcpy (p, );
Printf ("% s/n % s", p,)
Compilation will not go wrong, and the running result is
123456
56
It is found that p does get the desired value, but a is actually modified.
The reason is that the specific operation during replication is as follows:
The memory structure before replication is p, and a is after: '/0'/0'/0'/0'/0' '1' 1' 2' '3' '4' 5' '6'/0'
P points to the First '/0', and a points to '1 '. although declared as p [2], due to "memory alignment", the compiler automatically adds two characters and fills in '/0'
The memory structure after replication is still p before, and a after: '1' 2''3' '4''5' 6''/0''4''5' 5'/0'
P and a do not change the memory address, so p points to '1' and a points to '5'
Therefore, the above error occurs.

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.