Thread: A few questions

  1. #1
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72

    A few questions

    Hey guys,

    I am an EE that has found myself doing more and more programming lately, so I have a small question for you guys. I have a good understanding of pointers, but I don't really understand the reasoning behind this:

    Code:
    int i, index = *((int*)ptr);
    Why are both the * operators necessary? I've seen this around a bunch, and it seems like it's a pointer to a pointer? Is that correct? I don't really understand it. What is the 'name' of this technique, and what is it used for?

    Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    (int*) says "pretend, for the moment, that ptr is an int*". * means "go to the address held by this pointer and see what's there".

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I just started using this one (presuming that ptr is actually a char*). It works if you want to pull a number out of a block of data if the number was stored as an integer, and you know the offset. Unless the block is just an array of ints, you cannot simply use an int pointer -- eg, a struct containing an int:
    Code:
    struct {
           char string[64];
           int num;
    } *somestruct;
    Internet packets are constructed this way, but unless you can fit a struct onto them, you can't access what would be "somestruct->num". But you can do this:
    Code:
    struct somestruct *sp;   
    char *cp=(char*)sp+64;
    int X=*((int*)cp);
    Don't ask me how to explain it completely (I think tabstop already did), however, as I probably tried every arrangement of asterisks and parentheses I could think of before it worked (and this is probably the only way it works). Or maybe I got it from some kind of google.

    So the fact that you've seen it somewhere else is comforting...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    it's not a pointer to a pointer but one that has been cast appropriately and then dereferenced.

  5. #5
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72
    Quote Originally Posted by tabstop View Post
    (int*) says "pretend, for the moment, that ptr is an int*". * means "go to the address held by this pointer and see what's there".
    I sort of see what you are saying, but why won't this work?

    int i, index = (int)*ptr
    I don't see why the casting needs a dereference operator. Is it casting the value as an int pointer type, as opposed to just an int type?

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Cell View Post
    I don't see why the casting needs a dereference operator. Is it casting the value as an int pointer type, as opposed to just an int type?
    that's exactly it.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Cell View Post
    I sort of see what you are saying, but why won't this work?



    I don't see why the casting needs a dereference operator. Is it casting the value as an int pointer type, as opposed to just an int type?
    let's say ptr is void* then *ptr is just not defined

    let's say ptr is char*
    then *ptr is a contents of 1 char, and you cast it to int later

    *(int*)ptr is contents of 4 (maybe 8 on some compilers) bytes interpreted as int

    don't you think that the contents of 1 byte will be slightly different from the contents of 4 or 8 bytes?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72
    Quote Originally Posted by vart View Post
    let's say ptr is void* then *ptr is just not defined

    let's say ptr is char*
    then *ptr is a contents of 1 char, and you cast it to int later

    *(int*)ptr is contents of 4 (maybe 8 on some compilers) bytes interpreted as int

    don't you think that the contents of 1 byte will be slightly different from the contents of 4 or 8 bytes?
    I see what you're saying. I did not realize that it was casting an int pointer type and not just an int type.

    Thanks for the help, everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM