The difference between a string pointer and a character array (i)--the value of a string constant cannot be changed

Source: Internet
Author: User

Note: This is not very clear about the concept, should not be called a string pointer, C language does not exist the string pointer this data type.

Often make mistakes in the knowledge point, before also did not understand, here again good record. Always be warm and know new.

Both character arrays and character pointer variables enable the storage and operation of strings. But there is a difference between the two. The following issues should be noted when using:

1. The string pointer variable itself is a variable that holds the first address of the string. The string itself is stored in a contiguous memory space, headed by the first address, and ends with a ' yes ' string. Character arrays are composed of several array elements that can be used to hold the entire string.

2. For string pointer mode

Char *ps= "C Language";

Can be written as:

Char *ps;

ps= "C Language";

and the array mode:

static char st[]={"C Language"};

cannot be written as:

Char st[20];

st={"C Language"};

You can only assign values to individual elements of a character array.

From the above, you can see that the string pointer variable differs from the character array when it is used, but also that it is more convenient to use the pointer variable.

It is dangerous to use a pointer variable before it obtains a definite address, which can easily cause errors.
An example of the error is as follows:
Char *name;
scanf ("%s", name);
printf ("%s", name);
Some compilers can pass, but this is wrong because it is a pointer that points to an unavailable address when defined. There are two ways to solve this problem: An array method or a method of allocating memory space to a character needle.
Methods of arrays:
Char name[20];
scanf ("%s", name);
printf ("%s", name);

Ways to allocate memory space to a character needle:
Char *name;
Name= (char*) malloc (50); At this point, name already points to a newly allocated address space.
scanf ("%s", name);
printf ("%s", name);

However, it is possible to assign a direct value to a pointer variable. Because the C system assigns a value to a pointer variable, it is given a definite address. 3.int Main ()
{
Char str1[40]= "Hello world!"; Char *str1= "Hello world!";
str1[4]= ' A '; If the str1 is a pointer type, the compiler passes, but the run is a section error here
printf ("%s\n", str1);
return 0;
}


Arrays and pointers can be initialized with string constants in their definition, although it looks like the underlying implementation mechanism is different.

When you define a pointer, the compiler does not allocate space for the object that the pointer points to, it simply allocates space for the pointer itself, unless you initialize the pointer with a string constant at the same time as the definition. For example, the following definition creates a string constant (memory allocated to it):

Char *p= "ABCDEFG";

Note This is true only for string constants, and you cannot expect to allocate space for constants such as floating-point numbers, such as:

float *p=3.14; /* error, unable to compile/* *

The following is an example of the difference between a string constant created when the pointer is initialized and a string in an array: in ANSI C, the string constants created when the pointer is initialized are defined as read-only. If you try to modify the value of this string by using a pointer, an undefined behavior occurs. In some compilers, string constants are stored in text segments that are only allowed to be read, to prevent it from being modified. Arrays can also be initialized with string constants:

Char a[]= "ABCDEFG";

As opposed to pointers, arrays initialized by string constants can be modified. A single character can be changed later.

The following is an example in VC6 that completes converting all uppercase letters in a string to lowercase letters:

#include <iostream.h>
 #include <ctype.h>
  
   /************************************************** /
   * Convert A string to lower case * */
  
  int strlower (char *string)
 {
     if (string==null)
     {
         return-1
   }
 
     while (*string)
     {
         if (Isupper (*string))
             *string=tolower (*string);
         string++;
     }
     *string= ' ";
     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 = ' n ';
     return string;
 }
 *

 /void Main ()
 {
     char *test= "ABCDEFGHIJKLMN";
     Strlower (test);
     cout<<test<<endl;
 }

In which, if a char *test= "ABCDEFGHIJKLMN" is used, a run-time error is generated. Char test[]= "ABCDEFGHIJKLMN" The program works as explained earlier.


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.