Thread: [Newbie] Casting question

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    10

    [Newbie] Casting question

    This construct gives a compiler warning with gcc, saying that casting in an lvalue is deprecated.


    Code:
    if((DWORD)rtp & 1)
        ((char *)rtp)++;
    Is this snippet equivalent?


    Code:
    if((DWORD)rtp & 1)
        rtp = (char *)rtp + 1;

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    What are you trying to do? This looks like an incredibly clumsy and dangerous punning effort.
    My best code is written with the delete key.

  3. #3
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47
    Yes they are equivalent. But why are you casting anyway without an lvalue? Your variable is already a char * so no need to cast. Why not just do rtp++; and forget the cast. You would only want to cast into an lvalue to cast into the type of the variable to the left of the assignment or to prevent truncation in math expressions. I don't believe it makes sense to have a cast without an lvalue as the variable is already that type. For example if you had a float f and did something like this:

    f = (float)123/(float)2;

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    10
    Well, I guess context is a good thing... this is straight from the WINE source (or see here). I'm trying to figure out what it's trying to do, so I can fix it and avoid the warnings (and hopefully learn in the process):


    Code:
    static void handle_ani_list(riff_tag_t *lst, enum res_e type, int isswapped)
    {
    	riff_tag_t *rtp = lst+1;	/* Skip the "LIST" tag */
    
    	while((char *)rtp < (char *)lst + lst->size + sizeof(*lst))
    	{
    		if(!memcmp(rtp->tag, info, sizeof(info)))
    		{
    			rtp = (riff_tag_t *)(((char *)rtp) + 4);
    		}
    		else if(!memcmp(rtp->tag, inam, sizeof(inam)))
    		{
    			/* Ignore the icon/cursor name; its a string */
    			rtp = NEXT_TAG(rtp);
    		}
    		else if(!memcmp(rtp->tag, iart, sizeof(iart)))
    		{
    			/* Ignore the author's name; its a string */
    			rtp = NEXT_TAG(rtp);
    		}
    		else if(!memcmp(rtp->tag, fram, sizeof(fram)))
    		{
    			/* This should be followed by "icon"s, but we
    			 * simply ignore this because it is pure
    			 * non-information.
    			 */
    			rtp = (riff_tag_t *)(((char *)rtp) + 4);
    		}
    		else if(!memcmp(rtp->tag, icon, sizeof(icon)))
    		{
    			handle_ani_icon(rtp, type, isswapped);
    			rtp = NEXT_TAG(rtp);
    		}
    		else
    			internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file",
    				       isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
    				       isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
    				       isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
    				       isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
    
    		/* FIXME: This relies in sizeof(DWORD) == sizeof(pointer_type) */
    		if((DWORD)rtp & 1)
    			((char *)rtp)++;
    	}
    }
    If I'm not mistaken, rtp is a pointer to a riff_tag_t object, and then is getting changed to a DWORD for some bitwise hacking, then into a pointer to a char to increment.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >rtp is a pointer to a riff_tag_t object, and then is getting changed to a DWORD for some bitwise hacking, then into a pointer to a char to increment.
    Yep, that's about as bad as I was thinking it was...

    [edit]
    And yes, they're equivalent.
    [/edit]
    Last edited by Prelude; 06-13-2004 at 08:08 PM.
    My best code is written with the delete key.

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    so bad coding is the reason why WINE is unstable for me

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, Windows is the reason Wine is unstable.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    10
    It seems from these responses that something more fundamental can be done to fix this problem, rather than just change the syntax to get rid of the warning.

    Any tips?

  9. #9
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by quzah
    No, Windows is the reason Wine is unstable.

    Quzah.
    lol nice one

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    10
    It seems like I would have to do this (change to pointer to char, add 1, change back to pointer to riff_tag_t):


    Code:
    rtp = (riff_tag_t *)(((char *)rtp) + 1);
    since the original rtp is a pointer to a riff_tag_t. Or does that happen automatically?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. The hilighted code doesn't modify anything, other than incrementing the pointer. It says:
    Code:
    if( thisvalue has thisbitpattern set )
        increment the pointer
    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Jun 2004
    Posts
    10
    Right, I understand that. My question is about the incrementing part. Is it


    Code:
    rtp = (char *)rtp + 1;
    or


    Code:
    rtp = (riff_tag_t *)((char *)rtp + 1);
    because rtp is of type 'riff_tag_t *' to begin with

  13. #13
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Quote Originally Posted by quzah
    No. The hilighted code doesn't modify anything, other than incrementing the pointer. It says:
    Code:
    if( thisvalue has thisbitpattern set )
        increment the pointer
    Quzah.
    It seems to change the pointer to point to a location one byte away from the previous location, it doesn't seem to be normal pointer incrementation.
    The one who says it cannot be done should never interrupt the one who is doing it.

  14. #14
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Quote Originally Posted by joeljkp
    Right, I understand that. My question is about the incrementing part. Is it


    Code:
    rtp = (char *)rtp + 1;
    or


    Code:
    rtp = (riff_tag_t *)((char *)rtp + 1);
    because rtp is of type 'riff_tag_t *' to begin with
    From 6.5.4.2
    Conversions that involve pointers, other than where permitted by the constraints of 6.5.16.1, shall be specified by means of an explicit cast.
    Looking up the forward reference
    6.5.16.1 Simple assignment
    Constraints
    1 One of the following shall hold:
    — the left operand has qualified or unqualified arithmetic type and the right has arithmetic type;

    — the left operand has a qualified or unqualified version of a structure or union type compatible with the type of the right;

    — both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;

    — one operand is a pointer to an object or incomplete type and the other is a pointer to a qualified or unqualified version of void, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;

    — the left operand is a pointer and the right is a null pointer constant; or

    — the left operand has type _Bool and the right is a pointer.
    In this case, the casting you suggested seems necessary.
    The one who says it cannot be done should never interrupt the one who is doing it.

  15. #15
    Registered User
    Join Date
    Jun 2004
    Posts
    10
    Interesting. From what is that quoted? The C99 spec?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Question on casting 0
    By laertius in forum C Programming
    Replies: 2
    Last Post: 11-09-2008, 12:15 PM
  3. question about casting pointers/other types also??
    By newbie02 in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2003, 05:01 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM