The difference between using a string pointer variable and a character array

Source: Internet
Author: User

Both character arrays and character pointer variables can be used to store and operate strings, but there is a difference between the two. When using, you should pay attention to the following issues:

1) The string variable itself is a variable that holds the first address of the string. The string itself is stored in a contiguous memory space at the first address and ends with "s" as a string. Character arrays are composed of several array elements that can be used to hold the entire string.

2) How to assign values. A character array can only be assigned to individual elements, and a character array cannot be assigned a value in the following way.

For a character pointer variable, you can assign a value using the following method.

3) The character array consists of several elements, one character in each element, and the character pointer variable holds the address, not the string into the character pointer variable.

4) If a character array is defined, it is assigned a memory unit at compile time and it has a definite address. When defining a character pointer variable, assign a memory unit to the pointer variable, where you can put an address value. That is, the pointer variable can point to a character data, but if it is not assigned an address value, she does not specifically point to a certain character data, so this method is not advocated.

5) The value of the pointer variable can be changed, although the array name represents the address, but its value cannot be changed. You can use the subscript form to reference the characters in the specified string.

6) Use a pointer variable to point to a format string, which can be used instead of a format string in the printf function. It can also be implemented with a character array, but it cannot be assigned as a whole using an assignment statement. For example:

The difference between using a string pointer variable and a character array

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.