Differences between string pointers and character Arrays

Source: Internet
Author: User

A string pointer variable is a variable used to store the first address of a string. The string itself is stored in a contiguous memory space headed by this first address and ended with '\ 0. Character array is composed of several array elements, which can be used to store the entire string.

It is dangerous to use a pointer variable before obtaining a definite address. .
An error example is as follows:
Char * Name;
Scanf ("% s", name );
Printf ("% s", name );
  Some compilers can pass, but this is incorrect,It is a pointer and points to an unavailable address during definition.. There are two ways to solve this problem:Array MethodOrMemory space allocated by the character needle.
Array method:
Char name [20];
Scanf ("% s", name );
Printf ("% s", name );

How to allocate memory space to the character needle:
Char * Name;
Name = (char *) malloc (50); // The name already points to the allocated address space.
Scanf ("% s", name );
Printf ("% s", name );

However, you can assign values to pointer variables directly. This is because the C system must give a definite address when assigning values to pointer variables.The String constant created during pointer Initialization is defined as read-only.

Int main ()
{
Char str1 [40] = "Hello world! "; // Char * str1 =" Hello world! ";
Str1 [4] = 'a'; // If str1 is pointer-type, It is compiled, but it will be an error when running
Printf ("% s \ n", str1 );
Return 0;
}

 

Arrays and pointers can both be initialized with string constants in their definitions. Although they look the same, the underlying implementation mechanisms are different.

When defining pointers,The compiler does not allocate space for the object to which the Pointer Points. It only allocates space for the pointer.Unless you assign a value to a String constant at the same time of definition for initialization. For example, the following definition creates a String constant (allocated memory): char * P = "abcdefg ";

Note:

  1. In ANSI C,String constantIs defined as read-only.If you try to modify the value of this string through a pointer,ProgramThen there will be undefined behaviors. In some compilers, A String constant is stored in a text segment that can only be read to prevent modification.
  2. The array can also be initialized using a String constant: Char A [] = "abcdefg"; in contrast to the pointer, the array initialized by a String constant can be modified. A single character can be changed later.
  3.  
    The following is an example in vc6 to convert all uppercase letters in a string to lowercase letters:
    Int Strlower ( Char *String ){ If ( String = Null ){ Return - 1 ;} While (* String ){ If (Isupper (* String )) *String = Tolower (* String ); String ++ ;} * String = ' \ 0 ' ; Return 0 ;} /* Char * strlower (char * string) {char * s; If (string = NULL) {return NULL;} s = string; while (* s) {If (isupper (* s) {* s = (char) tolower (* s);} s ++;} * s = '\ 0'; return string ;} */ Void Main (){ Char * Test = " Abcdefghijklmn " ;// Defines a String constant and cannot be changed ..............................Strlower (TEST); cout <Test <Endl ;}
    If char * test = "abcdefghijklmn" is used, a runtime error is generated. Char test [] = "abcdefghijklmn" indicates that the program runs normally. The reason is as described above.

     

Arrays and pointers can both be initialized with string constants in their definitions. Although they look the same, the underlying implementation mechanisms are different.

When defining pointers,The compiler does not allocate space for the object to which the Pointer Points. It only allocates space for the pointer.Unless you assign a value to a String constant at the same time of definition for initialization. For example, the following definition creates a String constant (allocated memory): char * P = "abcdefg ";

Note:

  1. In ANSI C,String constantIs defined as read-only.If you try to modify the value of this string through a pointer, the program will be undefined. In some compilers, A String constant is stored in a text segment that can only be read to prevent modification.
  2. The array can also be initialized using a String constant: Char A [] = "abcdefg"; in contrast to the pointer, the array initialized by a String constant can be modified. A single character can be changed later.
  3.  
    The following is an example in vc6 to convert all uppercase letters in a string to lowercase letters:
    Int Strlower ( Char * String ){ If ( String = Null ){ Return - 1 ;} While (* String ){ If (Isupper (* String )) * String = Tolower (* String ); String ++ ;} * String = ' \ 0 ' ; Return 0 ;} /* Char * strlower (char * string) {char * s; If (string = NULL) {return NULL;} s = string; while (* s) {If (isupper (* s) {* s = (char) tolower (* s);} s ++;} * s = '\ 0'; return string ;} */ Void Main (){ Char * Test = " Abcdefghijklmn " ;// Defines a String constant and cannot be changed ..............................Strlower (TEST); cout <Test < Endl ;}
    If char * test = "abcdefghijklmn" is used, a runtime error is generated. Char test [] = "abcdefghijklmn" indicates that the program runs normally. The reason is as described above.

     

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.