search
Homephp教程PHP开发Detailed explanation of the use of character arrays and strings in C language

1. Definition and initialization of character array
The easiest way to understand the initialization of character array is to assign characters to each element in the array one by one.
char str[10]={ 'I',' ','a','m',' ','h','a','p','p','y'};
That's it The 10 characters are assigned to the 10 elements from str[0] to str[9] respectively. If the number of characters provided in the curly braces is greater than the array length, it will be treated as a syntax error; if it is less than the array length, only these characters will be included in the array. For the first elements, the remaining elements are automatically set to empty characters (i.e. '

3. String representation
In C language, you can use two methods to represent and store strings:
(1) Use a character array to store a string
    char str[ ]="I love China";
( 2) Use a character pointer to point to a string
    char* str="I love China";
For the second representation method, some people think that str is a string variable, and they think that the string constant "I love China" should be directly defined when defining it. Assigning to the string variable is incorrect.
C language handles string constants as character arrays. A character array is opened in the memory to store string constants. When the program defines the string pointer variable str, it only changes the first address of the string (that is, the address where the string is stored). The first address of the character array) is assigned to str.
String output in both representation methods uses
printf("%sn",str);
%s means outputting a string, giving the character pointer variable name str (for the first representation method, the character array name is is the first address of the character array, which is consistent with the meaning of the pointer in the second type), the system first outputs the character data it points to, and then automatically adds 1 to str to point to the next character..., This continues until the end-of-string identifier "

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor