Thread: pointers

  1. #1
    Registered User
    Join Date
    Sep 2009


    what does this statement do *(unsigned short)*a

  2. #2
    Registered User
    Join Date
    Sep 2007
    That's a constraint violation (aka syntax error).

    Presumably you mean:
    *(unsigned short *)a;
    This converts a to a pointer to unsigned short and then dereferences it, giving you the value of the unsigned short int at that address.

    This could be useful if a is, say, a pointer to unsigned char/void that is used as a generic pointer to some chunk of memory. You, for one reason or another, know that the pointer actually points to a short (or at the very least you want to treat the value at the address as a short). You can't dereference a directly because it'll either give you a byte (if it's an unsigned char pointer) or it'll be invalid (if it's a void pointer). So you tell the compiler to pretend a is a pointer to an unsigned short, and then dereference it. It's a shorthand for:
    unsigned short *s = (unsigned short *)a; /* cast may or may not be necessary */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM