character arrays and character pointers

Source: Internet
Author: User

1. The character pointer can point to a string.

We can initialize the character pointer with a string constant. For example, there is a description statement:

Char " This is a string. ";

is to initialize the character pointer. At this point, the character pointer points to the first address of a string constant, which points to the first address of the string.

Notice the difference between the character pointer and the character array. For example, there is a description statement:

Char string []="This isa string. ";

At this point, string is an array of characters, which holds a string.

character Pointer Str with character array string The difference isthat STR is a variable that can change STR to point to different strings, but cannot change the string constants that Str refers to. A string is an array that changes the contents of the array to be saved.

2. Example:

Char *str, *str1="This isanother string. " ; Char string []=]This isa string. ";

In the program, you can use the following statement:

str++;/*pointer str plus 1*/Str="This is a NEW string.";/*to point the pointer to a new string constant*/Str= STR1;/*change pointer to str pointing*/strcpy (string,"This is a NEW string.")/*Change the contents of a string*/strcat (string, str)/*make a string connection operation*/

In the program, you cannot do the following:

string++;/*cannot perform + + operations on array names*/string="This is a NEW string.";/*Bad string Operation*/string= STR1;/*the array name cannot be assigned*/strcat (str,"This is a NEW string.")/*cannot connect to a string after str*/strcpy (str,string)/*cannot string copy to Str*/

3 . Other instructions:

1) in the form of a string, the compiler will automatically add a 0 as the Terminator for the string, as in code: "ABC", then the compiler helps you to store "abc/0"

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

  is not a constant : "ABC" as the initial value of the character array is not, as

Char " ABC ";

Because the definition is a character array, so it is equivalent to define some space to hold "abc", and because the character array is to put the character one by one, so the compiler parses this statement to char str[3] = {' A ', ' B ', ' C '}, and according to the above summary 1, So char str[] = "abc"; The end result is char str[4] = {' A ', ' B ', ' C ', '/0 '};

To do an extension, if char str[] = "ABC"; it is written inside the function, then "abc/0" here is not a constant, so it should be placed on the stack.

is a constant condition: when assigning "abc" to a character pointer variable, such as

Char " ABC ";

Because the definition is a normal pointer, and does not define space to hold "abc", so the compiler has to help us find a place to put "ABC", obviously, the "ABC" here as a constant and put it in the program's constant area is the compiler's most appropriate choice. So although the type of PTR is not const char* and ptr[0] = ' x '; can also be compiled through, but execute ptr[0] = ' x '; a run-time exception occurs because this statement attempts to modify something in the program's constant area.

Remember which book has ever said char* ptr = "abc", which originally is not allowed in the C + + standard, but because this writing in C is too much, in order to compatible with C, not allowed to be allowed. Although allowed,

But the suggested wording should be const char* PTR = "abc"; So if the back is written ptr[0] = ' x ' The compiler will not let it compile, and it avoids the run-time exception mentioned above.

Again, if char* ptr = "abc"; written in the function body, then although the "abc/0" here is

is placed in a constant area, but PTR itself is just a normal pointer variable, so PTR is placed on the stack, but the object it points to is placed in the constant area.

3) The type of the array is determined by the type of things that the array holds and the size of the array itself. Types such as Char s1[3] and char s2[4],s1 are the CHAR[3],S2 type char[4], which means that although S1 and S2 are character arrays, the types are different.

4) The type of the string constant can be understood as the type of the corresponding character constant array, such as the type of "abcdef" can be regarded as a const CHAR[7]

5) sizeof is used to find the number of bytes of the type. such as int A; then either sizeof (int) or sizeof (a) is equal to 4, because sizeof (a) is actually sizeof (type of a)

6) for formal parameters written in the array type in the list of function arguments, the compiler interprets it as a normal pointer type, such as for void Func (char sa[100],int Ia[20],char *p), the type of the SA is Char*,ia and is int*, The type of P is char*

7) According to the above summary, to combat a bit:

For char str[] = "abcdef", there is sizeof (str) = = 7, because the type of STR 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 type of PTR 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 are sizeof (sa) = = sizeof (IA) = = sizeof (p) = = 4,

Because the type of the SA is Char*,ia, the type of int*,p is char*.

4. Difference:

(1) A character array consists of several elements , each of which holds one character of the string, while the character pointer variable holds the first address of the string.

(2) The initialization method is different. initialization of character arrays is done with static storage classes, at compile time. The initialization of a character pointer variable does not have to be static when it is actually executed.

(3) are assigned in different ways. the character array cannot be assigned as a whole and can only be converted to a portion of a single element. The character pointer variable assignment can be performed as a whole.

For example:

Char s[];s=/"c++/";/* wrong, S is constant, How can I be assigned the value of * *

(4) when a character array is defined, the memory unit is allocated at compile time, with a definite address. when a character pointer variable is defined, the pointer variable is assigned a memory unit, but the pointer variable points to which string, and it is not known that the pointer variable holds an indeterminate address. For example:

Char a[]; 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, and the character array name is a constant and cannot be changed. For example, there are simple programs:

void Main () {char *s= "China Man" s+=6; printf ("%s", s);}

Running Result: Man

character arrays and character pointers

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.