pointers

This is a discussion on pointers within the C Programming forums, part of the General Programming Boards category; what does this statement do *(unsigned short)*a...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    1

    pointers

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

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

    Presumably you mean:
    Code:
    *(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:
    Code:
    unsigned short *s = (unsigned short *)a; /* cast may or may not be necessary */
    *s;

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 Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21