Thread: Quick const question

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    3

    Quick const question

    Why does this throw an exception?
    Code:
    int isOdd_8(const unsigned int * pArg)
    	{
    
    	return *pArg & 1;
    	}
    It works fine if I use a temporary variable to store pArg, but it seems that I can't do any operations on consts.
    Is a temporary variable the way to go?


    I can't find any definitive answers on Google.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What do you mean by "throw an exception"? If I copy that code, it compiles and runs perfectly well on my system.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    3
    I'm getting "Access violation reading location 0x000000aa" when I give it a value of AA.

    Actually, that looks like I've just messed up the pointer, but when I change it to this:
    Code:
    return pArg & 1;
    it won't compile, giving me this error:

    error C2296: '&' : illegal, left operand has type 'const unsigned int *'

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I think the error is on the other side -- are you sure you're passing a pointer, and not just an unsigned int? (That is to say, you need to call this as isOdd_8(&intvar).)

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    3
    Oh, awesome. That one slipped by me, that was it.

    Thanks.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I'm amazed that you managed to call isOdd_8( 0xAA ); without getting any warnings either.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template overloading question
    By plutino in forum C++ Programming
    Replies: 14
    Last Post: 02-27-2009, 02:10 AM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. const question
    By rip1968 in forum C++ Programming
    Replies: 2
    Last Post: 05-17-2002, 07:29 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM