The difference between a C + + string pointer and a character array

Source: Internet
Author: User

Today we find such a problem
#include <iostream>using namespacestd;intMain () {Charch1[Ten]; strcpy_s (CH1,"123456");//compiled by    Char* p =New Char[Ten]; strcpy_s (P,"123456");//Error: 2 parameters not accepted}

The change to the following is no problem:

strcpy_s (P,ten,"123456"); // compiled by

Why is that?

First look at the difference between a character array and a string pointer:

1. A string constant enclosed in double quotation marks is a static storage type that is stored in a static storage area of memory, so no matter where the string constant appears in the program, it stores only one copy of the program during the entire run.

If you initialize a character array with string constants, you copy the entire string from the static store to the array. For example:

Char " Hello ";

If the character pointer is initialized with a string constant, the address of the string in the static store is copied to the pointer. For example:

Char "  World ";
The two forms of an important differenceThe value of a string cannot be modified by a pointer, but character ArrayThis problem does not exist, such as

The statement is wrong:

p[0'P';

There is no problem with the following statement:

a[0'a';

To avoid the error of modifying strings with pointers, we recommend that you declare the character pointer as a const type, as follows:

Const Char "  World ";

For example:

. int   main () { char  str1[40 ]="  hello world!       "; //  char *str1= "Hello world!";  Str1[4 ]= "                       A   "; //  printf (  Span style= "COLOR: #800000" >%s\n   " ,str1);  return  0   ; }

2. Arrays and pointers can be initialized with string constants in their definitions, although the underlying implementation mechanism is different, even if they look the same.

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

Char *p= "ABCDEFG";

Note that 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=2.34;  /* error, cannot compile by */

The following is an example of the difference between a string constant created when initializing a pointer and a string in an array:

    1. In ANSI C, the string constants that are created when the pointer is initialized are defined as read-only. If you attempt to modify the value of this string through a pointer, the program will have undefined behavior. In some compilers, string constants are stored in only the text segments that are allowed to be read to prevent it from being modified.
    2. Arrays can also be initialized with string constants:
      Char a[]= "ABCDEFG";

An array initialized by a string constant can be modified, as opposed to a pointer. One of the individual characters can be changed later.

The following is an example in VC6, which completes all uppercase letters in a string into lowercase letters:

#include <iostream>#include<ctype.h>using namespacestd;/******************************************************************************/  /** Convert A string to lower case*/  intStrlower (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 = ' + '; return string; } */voidMain () {Char*test="ABCDEFGHIJKLMN";      Strlower (test); cout<<test<<Endl; }

where char *test= "ABCDEFGHIJKLMN" is used, a run-time error is generated. Char test[]= "ABCDEFGHIJKLMN" The program runs correctly for the same reason as described earlier.

The difference between a C + + string pointer and a character array

Related Article

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.