C ++'s memset Function

Source: Internet
Author: User

ZT: http://www.cppblog.com/zhangyq/archive/2009/01/06/71385.html

 

 

let's take a look at the following Code :
char array [12];
memset (array, 0, strlen (array);
int result = 0;
If (! Array)
{< br> result = 1;
}< br> Program is executed to the end, the value of result is still 0.
the memset function prototype is void * memset ( void * DEST , int C , size_t count ) ; it only sets the values of all elements in the memory block of DeST to C, it is not to set DEST to null (although null is defined as 0); null is a special type and its value is '/0 ', we can regard it as a pointer constant, but it is never null. The Compiler protects it. Most of us use it as a boundary condition. Memset (array, 0, strlen (aarray) only initializes the memory occupied by array with 0 instead of setting array to null; memset is only for the value of elements in a memory block, rather than the address. To set the result value to 1, the condition must be changed from array to array [0]. it is also important to note that we cannot change constants, otherwise an error will be reported, so when writing: char * Ch = "1234"; memset (CH, 0, strlen (CH), although the compilation can pass, but cannot run. The memset function uses the value transfer method to pass the parameter, because it is illegal to assign a constant to a variable, this will change the content of the constant.

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.