character arrays, character pointers, string constants, and some summary of sizeof

Source: Internet
Author: User

1, appears as a string, the compiler will automatically add a to the string as the end

If you write "abc" in your code, the compiler helps you store "Abc\0".

2. The type of the array is determined by the type of things that the array holds and the size of the array itself, such as Char s1[3] and char s2[4],s1 type is CHAR[3],S2 type is char[4]

The type of a string constant can be understood as the type of the corresponding character constant array, such as the type of "ABC" can be regarded as a const char[4]

3. The difference between a character array and a pointer

(1) "ABC" as the initial value of the character array, because the definition of a character array, so equivalent to define a space to hold "ABC", When the "abc\0" is not a constant, stored on the stack.

(2) when assigning "abc" to a character pointer variable, such as Char *ptr= "ABC", because a normal pointer is defined, and no space is defined to hold "ABC", "ABC" is stored in the program's constant area, so although the type of PTR is not const char*, and ptr[0]= ' x '; can also compile, but execute ptr[0] = ' x '; a run-time exception occurs because this statement attempts to modify something in the program's constant area. Therefore, the proposed notation is the const char *ptr= "ABC", so that if the subsequent write ptr[0]= ' x ', the compiler will not let it compile through, but also avoids the above said run is abnormal.

4, sizeof is used to find the type of the number of bytes

such as int A; then either sizeof (int) or sizeof (a) is equal to 4

5, for the function parameter list in the form of array type parameters, the compiler interprets it as a normal pointer type .

such as for void Func (char a[10],int ia[20],char *p) The type of a is the type of the Char*,ia type is int*,p is char*

Use sizeof for character arrays:

(1) for char str2[10] = "abcdef"; There is sizeof (str2) = = 10, because the type of STR2 is char[10]. --- array name does not degenerate into a pointer in sizeof, and is degraded to a pointer in strlen.

(2) for void func (char a[10],int ia[20],char *p);--------There is sizeof (a) = sizeof (IA) = = sizeof (p) = = 4, because the type of SA is the type of Char*,ia is I The type of nt*,p is char*. --- array names degenerate into pointers as function arguments, the function only passes the first address of the array and does not know the size of the array , so sizeof is 4

Note the difference between sizeof (array name) in both (1) and (2) cases!

character arrays, character pointers, string constants, and some summary of sizeof

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.