Character arrays and strings

Source: Internet
Author: User

Take the 32-bit system as an example.

1. Definition and initialization
Character array: char c[10];
String: char s[] = "Hello";

The definition and initialization of a character array can be together or separate:
Char c[10] = "Qwer"; Or
Char c[10];
C[0] = ' Q '; C[1] = ' W '; c[2] = ' e '; C[3] = ' R ';
Can't do this: c = "Qwer"
When initializing, be aware of the size of the array, while preserving the location of the.
When you print a character array with printf, you must have a/s in the array, or it will cause the print to cross.

The definition and initialization of a string cannot be separated, but the specified character can be modified, as
Char s[] = "Hello"; S[1] = ' o ';

2. SizeOf and Strlen
sizeof (c) = 10; is the actual array size
sizeof (s) = 6; Size is the number of characters +1 (1 is hide ' n ')
Strlen does not contain a:
Char c[10] = "Wwer"; Strlen (c) = 4;
Char s[] = "Helloz"; strlen (s) = 6;

3. References
Both are quoted in the same way, in two ways: by means of subscripts or pointers, such as:
Character array: c[1] = ' R ' or * (c + 1) = ' R '
String: s[1] = ' R ' or * (s + 1) = ' R '

4. String related operation function
There are mainly the following, which are basically defined in string.h. Linux system you can use the man command to see the use of each function. Such as:
[Email protected]:~/work/code/ctest$ man str
Strace strchr strcpy strerror strftime strlen strncpy strpbrk strsignal strtof strtol strtoul strverscmp
strcasecmp strchrnul strcspn strerror_r string strncasecmp strndup strptime strspn strtoimax strtold strtoull strxfrm
Strcasestr strcmp strdup Strfmon strings strncat strndupa strrchr strstr strtok strtoll Strtoumax
strcat strcoll strdupa strfry strip strncmp strnlen strsep strtod strtok_r Strtoq Strtouq
[Email protected]:~/work/code/ctest$ man strlen//view strlen Usage

Character arrays and strings

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.