Thread: pointer to a structure

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    70

    pointer to a structure

    given the code
    Code:
    struct DISK_REGISTER {
    unsigned ready:1;
    unsigned error_occured:1;
    unsigned disk_spinning:1;
    unsigned write_protect:1;
    unsigned head_loaded:1;
    unsigned error_code:8;
    unsigned track:9;
    unsigned sector:5;
    unsigned command:5;
    }
    can i have
    Code:
    struct DISK_REGISTER *disk_reg = &DISK_REGISTER_MEMORY;
    on the link
    gd.tuwien.ac.at/languages/c/programming-dmarshall/node13.html

    instead of the second piece of code, we have
    Code:
    struct DISK_REGISTER *disk_reg = (struct DISK_REGISTER *)DISK_REGISTER_MEMORY;

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well that's wrong. If "DISK_REGISTER_MEMORY" is a single instance of a structure, you can't just tell it that that instance is a pointer and expect it to work right. It doesn't work like that. Otherwise, you could do:
    Code:
    int x, *ptr;
    
    ptr = (int *)x;
    Which is clearly not correct. So, the answer is, if "DISK_REGISTER_MEMORY" is an instance of your structure, then your assignment you want to make would be correct. However, I didn't go to your link, so unless someone else feels like doing it...

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

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    70
    you mean my code is right
    and dmasrahll's code is wrong??

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's no way to say. Looking at the link, there is no place on it where they actually define "DISK_REGISTER_MEMORY", so there is no way of knowing what it is supposed to represent. You can't just say:
    To access values stored at a particular memory address, DISK_REGISTER_MEMORY we can assign a pointer of the above structure to access the memory via:

    struct DISK_REGISTER *disk_reg = (struct DISK_REGISTER *) DISK_REGISTER_MEMORY;
    And expect to be able to decypher what it is they're talking about. They never state what exactly "DISK_REGISTER_MEMORY" is. They should have defined it some place. I assume they're trying to say that "DISK_REGISTER_MEMORY" is a physical instance. I suppose what they meant to say is "If we have an instance of our DISK_REGISTER structure, called "DISK_REGISTER_MEMORY", then we can do...
    Code:
    struct DISK_REGISTER DISK_REGISTER_MEMORY;
    struct DISK_REGISTER *disk_reg;
    
    disk_reg = &DISK_REGISTER_MEMORY;
    In which case, they're correct. However, it's poorly worded. If that is in fact what they mean, then no, you can't do what you want to do.

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

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    70
    thanks dude
    what you says makes sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. method returning a pointer to a structure
    By nacho4d in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2009, 10:01 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. how to cast a char *mystring to a structure pointer ??
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 08:59 AM
  4. Pointer to a structure
    By frenchfry164 in forum C Programming
    Replies: 5
    Last Post: 03-16-2002, 06:35 PM
  5. Pointer to structure problem
    By unregistered in forum C Programming
    Replies: 3
    Last Post: 12-24-2001, 07:54 AM