The difference between a string and a character array

Source: Internet
Author: User

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";

For array mode, char st[]={"C Language"};

cannot be written as Char st[20];

st={"C Language"};

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

You can see the difference between using a string pointer variable and a character array, and you can see that using a pointer variable is just convenient.

However, pointer variables are dangerous to use before they are made to a certain address.

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 how the string constants created when the pointer is initialized are different from the strings in the 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.

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.