Char array with char pointer

Source: Internet
Author: User

1, in the form of a string, the compiler will automatically add to the end, think, why?

The presence of a C language method, such as strlen (s), computes the length of a string, where the s pointer is. Strlen to calculate the length of a string, you must know where the end is, so use the-s to indicate the end. Only the character array has the concept of a/s, and the array of other types (int) does not have this concept. This method is not strlen because of other types of arrays or pointers.

So the question is, how does the int array calculate the length? such as int a1 = {3,7,9,};

Use sizeof (A1)/sizeof (int).

2, the array can be allocated on the stack, can also be allocated on the heap, but must specify the size.

Char a1[100]; Allocate on stack

char* PA = new char[100];//Allocated on the heap, returning the address of the first element

3, char a1[] = "ABC"; Equivalent to allocating 4 bytes at the top of the stack, respectively, placed on the a,b,c,\0, equivalent to char A1 ={' a ', ' B ', ' C ', ' n '};

4, char* PA = "abc"; Analysis, you know, PA is a char pointer, "ABC" is a text string, obviously type does not match, need to fit. It can be assumed that the compiler did the following: Allocate 4 bytes in the constant area, put the a,b,c,\0 on each, and then return the address of a to the PA.

Note: The text string is placed in the constant area and is not modifiable, attempting to modify and run the exception. Then think about it, since the right is a const, and PA is not limited to a const char*, the value of the argument fails. Why did you succeed?

You can think of this code everywhere in the C language. For compatibility, it must be allowed. However, we should write the const char* PA = "abc"; In this case, try to modify the PA content, compile error.

5, char a1[] = "abc", equivalent to char a1[] = {' A ', ' B ', ' C ', ' + ',}; strlen (A1) equals 3, length does not include

If so write char a1[] = {' A ', ' B ', ' C ',}; What is strlen (A1)? The answer is not certain, because strlen always finds the "end".
6, char a1[] = "ABC"; What are the following results, respectively?

cout<<&a1[0]<<endl;

cout<<a1<<endl;

The output is the same, which is the first address of the array element.

7, char* PA = "abc"; What are the following results, respectively?

cout<<&pa<<endl;

cout<<&pa[0]<<endl;

cout<<pa<<endl;

The first line outputs the address of the PA on the stack, the second row and the third row are the same, all the first addresses. The PA is the pointer, which is the address that points to the first element.

8, Char a1[5]; The array name is a pointer constant and cannot be modified to point to.

9, char* PA = "abc"; You can think of a PA as a pointer to a constant.

10, the following results, in violation of intuition, according to the 4th line, 5th line should be output address. But it outputs a string that points to. This is reasonable, we print the char pointer, is often to see the point of the content, rather than to see the address is how much. And cout is easy to do, as long as the encounter with the end. So the problem is, I want to see the address? Use int to force conversion to address.

1  Chara1[]="ABC";2  Char* pa="def";3 4cout<<a1<<endl;//OUTPUT ABC5cout<<pa<<endl;//Output def6 7cout<< (int) a1<<endl;//Output A1 Address8cout<< (int) pa<<endl;//Output PA Address

11.

1  Charp[]="ABCDE";2  Char* p2="ABCDE";3 4cout<<sizeof(p) <<endl;//array size is 65cout<<sizeof(p2) <<endl;//pointer size is 46 7Cout<<strlen (P) <<endl;//Length of 58Cout<<strlen (p2) <<endl;//Length of 5
View Code

Char array with char pointer

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.