C/C ++ array boundary and assignment

Source: Internet
Author: User
Declaration: Char Buf [5]; The element is a [0] a [1] A [2] a [3] a [4]
A [5] does not belong to the Buf array. This array can store up to four characters, a [0] a [1] A [2] a [3, because the character array contains 5th a [4] characters for/0

# Include <stdio. h>
# Include <unistd. h>
# Include <string. h>

Int main ()
{
Char STR [5];
Char c = 'D ';

Strcpy (STR, "hello"); // string copy (include '/0 ')

Printf ("D: % d C: % c str: % s/n", C, C, STR );

If (C = '/0 ')
Printf ("c = '// 0'/N ");
Else
Printf ("C! = '// 0'/N ");

Return 0;
}

[Root @ localhost CPP] #./
D: 0 C: Str: Hello
C = '/0' // visible strcpy fill in the memory space of STR [5], that is, char C, with/0

The correct method is

[Root @ localhost CPP] # Cat A. C
# Include <stdio. h>
# Include <unistd. h>
# Include <string. h>

Int main ()
{
Char STR [5];
Char c = 'D ';

 Strncpy (STR, "hello", sizeof (STR)-1); // STR copy (not including '/0 ', that is, the/0 is not automatically added to the end of the source string)
//
STR [4] = '/0 ';
Printf ("D: % d C: % c str: % s/n", C, C, STR );

If (C = '/0 ')
Printf ("c = '// 0'/N ");
Else
Printf ("C! = '// 0'/N ");

Return 0;
}

[Root @ localhost CPP] #./
D: 100 C: d str: Hell
C! = '/0'

Without STR [x] = '/0'
[Root @ localhost CPP] # Cat A. C
# Include <stdio. h>
# Include <unistd. h>
# Include <string. h>

Int main ()
{
Char STR [6];
Char c = 'D ';

Strncpy(STR, "hello", sizeof (STR)-1); // STR copy ('/0 ')
//
// STR [5] = '/0'; if you do not add/0, the following garbled error will occur, and the subsequent content of variable C is unpredictable.
Printf ("D: % d C: % c str: % s/n", C, C, STR );

If (C = '/0 ')
Printf ("c = '// 0'/N ");
Else
Printf ("C! = '// 0'/N ");

Return 0;
}

[Root @ localhost CPP] #./
D: 100 C: d str: hellosd0 commandid? Jo login so stupid, jo
C! = '/0' ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;when using the strcnpy function: char array [80]; const char * src = "sdjfksdfjksdf"; Method 1 strcnpy (array, SRC, sizeof (array); array [sizeof (array) -1] = '/0'; // to avoid SRC length exceeding 80 (including'/0'), '/0' is not added at the end of the array '. this causes a buffer overflow .;;;;;;;;; Method 2; memset (array, 0, sizeof (array); strncpy (array, SRC, sizeof (array)-1 ); // in this way, since memset adds array 0, ensure that array [sizeof (array) -1] = '/0 ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; no problem when the SRC length is smaller than 80. Note that strncpy () does not automatically add '/0'; unless you can ensure that the SRC length does not exceed the array length, you can directly use strcpy (array, Src );

 

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.