String Array Initialization

Source: Internet
Author: User
(1) Char arr [10] = "hi ";
When initializing a string array, the compiler automatically adds '\ 0' to the end of the string.
If the array size is greater than the length of the string plus 1, the elements after the end of the string are also initialized to '\ 0 ';
Read this section Program :
Defines a string array of 12 characters.
Char str1 [12] = "Jiajia ";
Point a char pointer to this array
Char * P1 = str1;
Output this string
Printf ("% s \ n", str1 );
Use pointers to output characters, ASCII values, and character storage addresses of each element in the string array.
For (INT I = 0; I <12; I ++)
{
Printf ("% C: % d: % P \ n", * P1, * P1, P1 );
P1 ++;
}
It can be seen that the last few digits of the array element are initialized to '\ 0'. When printf is used to print the string array, the program stops reading the string when it encounters the first' \ 0.

If the size of the array is smaller than the length of the string + 1, the program may encounter problems during runtime. Therefore, make sure that the size of the array is greater than the length of the string + 1;

The length of the string + 1 of this program is smaller than the size of the array:
Char str2 [12] = "Hello Jiajia ";
Char * P2 = str2;

Printf ("\ n % s \ n", str2 );

For (INT I = 0; I <28; I ++)
{
Printf ("% C: % d: % P \ n", * P2, * P2, P2 );
P2 ++;
}

To print a string using printf, the program needs to find '\ 0' to end reading. The result is not found in the entire array. The asⅱ on a specific address is always found to be 0 only after searching for the address, the string is read. Therefore, after reading "Hello Jiajia", this program may contain several random characters, because the string ends at '\ 0 '.

(2) Char arr [] = "Hello world! ";
This initialization method does not need to worry about the array size being too small. The size of the array is determined by the compiler.
(3) Char arr [] = {'h', 'E', 'E', 'l', 'O', '', 'w', 'O ', 'R', 'l', 'D', '\ 0 '};
The results of the second method are the same, but it is obviously a lot of trouble. In addition, ''indicates space, the ASCII value is 32, '\ 0' indicates the end of the string, and the ASCII value is 0;
(4) char * P = "Hello World ";
Use Arrays for initialization. The difference between arrays is that p is a variable that can be used for increment and decrease operations, while ARR is the first address of an array and an address constant. It cannot be used for increment or decrease operations.

Char * name = "liujiajia ";
Name [0] = 'l ';
Printf ("\ n % s \ n", name );
Name [0] = 'l ';CodeThe latest c99 standard may cause program exceptions. Note.

Char * string = "C programe ";
Char * string2;
String2 = string; // assign the string value of the pointer to string2

Printf ("\ nstring = % s, & string = % P, string = % P \ n", String, & string, string );
Printf ("\ nstring = % s, & string = % P, string = % P \ n", string2, & string2, string2 );

the above program shows that the address pointed to by the pointer string is the same as the address pointed to by the pointer string2, which indicates that they point to the same string in the memory, that is to say, the string itself is not copied, but a pointer to the same string is generated. In this way, the program will be more efficient. To copy a string, use strcpy () or strncpy ().

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.