dereferencing char pointers
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: dereferencing char pointers

  1. #1
    Join Date
    Jun 2012
    Posts
    58

    dereferencing char pointers

    hi,

    why doesnt the following program work as expected:


    Code:
    	char x = 0xff;
    	char* y = &x;
    	if(*y == 0xff)
    	{
    		return 1;
    	}
    	return 0;
    imo, it should return "1", but it doesnt. It seems like instead of comparing 0xff == 0xff, the compiler compares 0xffffffff == 0xff. Why?

    If i use "byte" for this example, everything works as expected, even though it`s just defined as an "unsigned char".


    Code:
    typedef unsigned char	byte;

  2. #2
    Join Date
    Oct 2008
    Posts
    1,077

    Re: dereferencing char pointers

    because char, signed char and unsigned char are different types and it's implementation defined whether char is signed or not. More specifically, the integer literal 0xff has type int, which is converted to char that, being signed in your compiler implementation, acquires the integer value -1. Then, in the expression "*y==0xff" where you're comparing a char and an int, integer promotion rules kick in and the char is converted to the int -1, giving the observed result.

  3. #3
    Join Date
    Jun 2012
    Posts
    58

    Re: dereferencing char pointers

    thanks.
    in that case, char should be unsigned by default, imo.

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: dereferencing char pointers

    That would be handy in some cases but also quite confusing since the other integers (short, int...) are signed. There are a lot of pitfalls in C/C++ and integer promotion will create nasty surprises for you in many other cases as well. It's just to get used to it...
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,003

    Re: dereferencing char pointers

    Quote Originally Posted by tuli View Post
    why doesnt the following program work as expected:
    Two guidelines that can help to prevent these kinds of errors are:
    - Don't use magic numbers. If you use a const variable for the value 0xff, you make the type of that variable explicit.
    - Don't mix signed and unsigned arithmetics. Signed values will get promoted to unsigned, which can cause unexpected results.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center