What does string assignment mean?

藏色散人
Release: 2019-06-04 10:19:17
Original
9955 people have browsed it

Strings are mainly used for programming. Strings are similar to character arrays in storage, so a single element of each bit can be extracted. For example, s="abcdefghij", then s[1]="a", s[9]="j", and the zero position of the string is exactly its length, such as s[0]=10 (※Ansistring does not have the above function.) This can provide us with a lot of convenience, such as high-precision operations Each bit can be converted into a number and stored in an array.

What does string assignment mean?

#But string assignment has certain requirements. For example, C language operators cannot operate strings at all. In C language, strings are treated as arrays. Therefore, strings are subject to the same restrictions as arrays. In particular, they cannot be copied and compared using C language operators.

String assignment method

In C language, there are two forms of string expression: one is in the form of a character array, such as char str[20] ="I love china"; The other is in the form of a character pointer, such as char *str="I love china". Strings can be assigned to character pointer variables, or strings can be stored in character arrays. Because the C language does not directly provide syntactic sugar support for strings. Moreover, many methods of the C standard library process strings based on the null character termination, which must be remembered.

char *p,a='5';
p=&a;                     //显然是正确的,
p="abcd";              //但为什么也可以这样赋值??
Copy after login

In the above program, double quotes do three things: apply for space (in the constant area) and store the string; add '/0' at the end of the string; return address. Here, the returned address is assigned to p.

But why does the char *p = “hello”; expression work, but changing p into an array and then assigning a value does not work. This is because when the string constant "hello" appears in an expression, the value used by the "hello" expression is the address where these characters are stored (in the constant area), not the characters themselves.

So, you can assign a string to a pointer p that points to a character, but you cannot assign a string to a character array.

char a[10] = “hello”; This is also possible. This situation is supported by c language initialization. If it is written as char a[10] then a = "hello" this is wrong. It is also an array of a, char a[10] = “hello”; this is the initialization of the array, and it is the same as a[0] = ‘h’ a[1] = ‘e’….

But changing to char a [10], then a = "hello" will not work. The value assigned to "hello" is an address, and although a also has an address, this is different from a pointer. A pointer The value of is an address, and although the value of the array is also an address, it is a constant, so a constant cannot be assigned a value. [3]

Let’s test it:

#include <stdio.h> int main(){       char *p = "hello";        printf("%s",p);       
        char a[10];        a = "hello";      
        return 0;}error C2440: &#39;=&#39; : cannot convert from &#39;char [6]&#39; to &#39;char [10]&#39;        There is no context in which this conversion is possible
Copy after login

When you see such an error message, try changing char a[10] to char a[6]

error C2106: ‘=’ : left operand must be l-value
Copy after login

The left side of the operator should be an "lvalue". The so-called "lvalue" refers to the amount that occupies memory space in the program and can be modified, such as various variables.

When using a pointer, the pointer can be incremented, but the array name cannot be incremented. The compiler allocates space to the array, and the address of the array a represents a constant. Letting the constant increment itself is definitely not possible. .

At the same time, when the pointer is incremented, the compiler will automatically identify the type. For example, if the pointer points to int type, if you want to get the next address, just p the pointer. Don't bother with p 4. .

It should be noted that when using void pointers, pointer arithmetic cannot be used, because the void compiler cannot recognize the length of the type (that is, the volume of the object pointed to by the pointer). p This is illegal. That is to say, mathematical operations cannot be performed, and the * value operation cannot be used. If you want to use it, you must convert it to other types.

The above is the detailed content of What does string assignment mean?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template