C character array assignment.

Source: Internet
Author: User

From: http://blog.csdn.net/jphaoren/article/details/5803583

Example:

Char A [10];
1. assign values directly using strings during definition
Char A [10] = "hello ";
Note: you cannot assign a value to it first, as shown in figure
Char A [10];
A [10] = "hello ";
This is wrong!

2. assign values to characters in the array one by one
Char A [10] = {'h', 'E', 'l', 'l', 'O '};

3. Use strcpy
Char A [10];
Strcpy (a, "hello ");

Error prone:
1. Char A [10]; A [10] = "hello"; // how can a character hold a string? Besides, a [10] does not exist!
2. Char A [10]; A = "hello"; // This situation is prone. Although a is a pointer, it has pointed to the 10 character space allocated in the stack, now, in this case, A points to the hello constant in the Data zone. Here, pointer A is messy and not allowed!

Also, you cannot use the relational operator "=" to compare two strings. You can only use the strcmp () function for processing.

Operators in C language cannot operate strings at all. In C language, strings are treated as arrays. Therefore, the restrictions on strings are the same as those on arrays. In particular, they cannot be copied or compared using operators in C language. Directly copying or comparing strings will fail. For example, assume that str1 and str2 have the following statements:

Char str1 [10], str2 [10];

It is impossible to copy a string to a character array using the = Operator:

Str1 = "ABC";/***** wrong ***/

Str2 = str1;/*** wrong ***/

The C language interprets these statements as an (invalid) value assignment between a pointer and another pointer. However, using = to initialize the character array is legal:

Char str1 [10] = "ABC ";

This is because in the declaration, = is not a value assignment operator.

It is legal to try to use Relational operators or judgment operators to compare strings, but it will not produce the expected results:

If (str1 = str2).../*** wrong ***/

This statement compares str1 and str2 as pointers, rather than comparing the content of the two arrays. Because str1 and str2 have different addresses, the value of str1 = str2 must be 0.

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.