Use memset to set 0xff for a char, and then set the char and 0xff =, can they be equal?

Source: Internet
Author: User
# Include <stdio. h>
# Include <string. h>

Int main ()
{
Char;
Memset (& A, 0xff, 1 );
If (A = 0xff)
Printf ("Equal. \ n ");
Else
Printf ("not equal. \ n ");
Return 0;
}

The answer is not equal. It can be seen from GDB that the variable in the memory is indeed 0xff, because it is set using memset. However, since a is of the char type and can only be 0x7 f at the maximum, 0xff has exceeded the char semantics when = is compared, equality is not true. If a is an unsigned char, It is equal.

In programming, char * is often used to represent a piece of memory. Therefore, be sure to pay attention to the signed character of char. Otherwise, an error will occur when char is used for memory comparison. Therefore, if char * is used to represent a piece of memory rather than a string, we strongly recommend that you use unsigned char * instead!

 

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.