Differences and relationships between character arrays and character pointers

Source: Internet
Author: User

1. The character pointer can point to a string.

We can use string constants to initialize character pointers. For example, there are instructions:

Char * STR = "this is a string .";

Is to initialize the character pointer. In this case, the character Pointer Points to the first address of a String constant, that is, the first address of a string.

Note the differences between character pointers and character arrays.For example, there are instructions:

Char string [] = "this is a string .";

In this case, string is a character array, which stores a string.

The difference between the character pointer STR and the character array string is:STR is a variable that can be changed to point to different strings, but cannot change the string constants referred to by Str. String is an array that can change the content stored in the array.

2. instance:

Char * STR, * str1 = "this is another string .";

Char string [100] = "this is a string .";

In the program, you can use the following statement:

STR ++;/* pointer STR plus 1 */

STR = "this is a new string.";/* point the pointer to a New String constant */

STR = str1;/* change the pointer to Str */

Strcpy (string, "This is a new string.")/* change the content of the string */

Strcat (string, STR)/* concatenate strings */

You cannot perform the following operations in a program:

String ++;/* You cannot perform ++ operations on Array names */

String = "this is a new string.";/* incorrect string operation */

String = str1;/* the array name cannot be assigned a value */

Strcat (STR, "This is a new string.")/* cannot be connected after Str */

Strcpy (STR, string)/* the string cannot be copied to Str */

3. Other Instructions:

1) If it appears in the form of a string, the compiler will automatically add a 0 as the end character for the string, such as: "ABC ", the compiler will help you store "ABC \ 0"

2) is "ABC" a constant? The answer is sometimes, sometimes not.

Not a constant: "ABC" is not used as the initial value of the character array, as shown in figure

Char STR [] = "ABC ";

Because it defines a character array, it is equivalent to defining some space to store "ABC", and because the character array stores the characters one by one, therefore, the compiler resolves this statement as char STR [3] = {'A', 'B', 'C'}. Based on the above summary 1, so the final result of char STR [] = "ABC"; is Char STR [4] = {'A', 'B', 'C', '\ 0 '};

If char STR [] = "ABC"; is written in the function, the "ABC \ 0" here should be placed on the stack because it is not a constant.

 Is a constant: When "ABC" is assigned to a character pointer variable, such

Char * PTR = "ABC ";

Because it defines a common pointer and does not define space to store "ABC", the compiler has to help us find a place to store "ABC". Obviously, taking "ABC" as a constant and placing it in the constant area of the program is the most appropriate choice for the compiler. Therefore, although the PTR type is not const char * And PTR [0] = 'X'; can also be compiled, run PTR [0] = 'X '; an exception occurs during running, because this statement attempts to modify things in the constant area of the program.

I remember which book once said char * PTR = "ABC"; this writing method was originally not allowed in the C ++ standard, but it is too much in C, to be compatible with C, it is not allowed. Although allowed,

However, we recommend that you write const char * PTR = "ABC". In this way, if PTR [0] = 'X' is followed, the compiler will not let it compile, this avoids the running exception mentioned above.

Expand again. If char * PTR = "ABC"; is written in the function body, although "ABC \ 0" is

Put it in the constant area, but PTR itself is just a common pointer variable, so PTR is placed on the stack, But it points to something put in the constant area.

3) The array type is determined by the type of the items stored in the array and the size of the array. For example, char S1 [3] and char S2 [4], S1 is Char [3], S2 is Char [4],

That is to say, although S1 and S2 are character arrays, their types are different.

4) The type of string constants can be understood as the type of the array of corresponding character constants,

For example, the "abcdef" type can be considered as const char [7].

5) sizeof is used to calculate the number of bytes of the type. For example, int A; so whether sizeof (INT) or sizeof (a) is equal to 4, because sizeof (a) is actually sizeof (type of)

6) for parameters written as arrays in the function parameter list, the compiler interprets them as common pointer types, for example, for void func (char SA [100], int Ia [20], char * P)

The SA type is char *, IA type is int *, and p type is char *

7) Based on the above summary, let's take a look at the actual situation:

For char STR [] = "abcdef"; there is sizeof (STR) = 7, because the STR type is Char [7],

There are also sizeof ("abcdef") = 7, because the type of "abcdef" is const char [7].

For char * PTR = "abcdef"; there is sizeof (PTR) = 4, because the PTR type is char *.

For char str2 [10] = "abcdef"; there is sizeof (str2) = 10, because the str2 type is Char [10].

For void func (char SA [100], int Ia [20], char * P );

There is sizeof (SA) = sizeof (IA) = sizeof (p) = 4,

Because the SA type is char *, IA type is int *, and p type is char *.

4. Differences:

(1) The character array consists of several elements., Each element contains a character of the string, and the character pointer variable stores the first address of the string.

(2) Different initialization methods.Static storage class is used for character array initialization, which is performed during compilation. The initialization of character pointer variables does not need to be static and will be performed in actual execution.

(3) assignment methods are different.You cannot assign a value to the character array as a whole. You can only convert the number of components to a single element. The character pointer variable value can be assigned as a whole.

For example:

Char s [10];

S = \ "C ++ \";/* error. S is a constant. How can it be assigned a value */

(4) when defining a character array,Memory units allocated during compilation,There is a definite address.When defining a character pointer variable, allocate a memory unit to the pointer variable. However, the pointer variable does not know which string it points,That is, the address where the pointer variable is stored is uncertain.For example:

Char A [10];
Char * P;
Scanf (\ "% s \", S);/* correct */

Scanf (\ "% s \", P);/* very dangerous, P value dynamic */

(5) The value of the character pointer variable can be changed,The character array name is a constant and cannot be changed. For example, there are simple programs:

# Include <stdio. h>
Int main ()
{
Char * s = "China man ";
S + = 6;
Printf ("% s \ n", S );
Getchar ();
Return 0;


}


Running result: Man

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.