Thread: dereferencing a void pointer

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    4

    dereferencing a void pointer

    I wrote the following routine to put an address into reg.r7 and then realized I needed not the address in reg.r7 but the value pointed to by the address. I'm trying to derefence this address but I'm stumped. What I coded still just puts the address into reg.r7

    Here is my function:

    Code:
    long convertDate(const void *date1) 
    {
       ProcessControl& control = getControlArea(); 
       TPF_regs regs; 
    
       if(date1 !=0) 
       { 
          regs.r7 = *(long *)date1; 
          control.input.location = DatWk::FIRST_R7;
       }
       UA80(&regs);
       return regs.r1;
    }

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    i don't think :: is a c operator

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Maybe *date1 is an address?

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    4
    :: is not the issue and works fine. The value in date1 is an address as I have passed by reference. Using the debugger I looked at the value in the storage location at the address in date1, it does indeed have the value which I want placed into regs.r7. Therefore, I do need to dereference date1. I know that since I passed a void pointer I need to tell the function what type this is a pointer to by using the type cast. Just for testing purposes I am testing the function with a long so I have type cast this as a long pointer and then tried to dereference... *(long *). I thought that would work but I continue to get the value in date1 instead of the data to which date1 points.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    long convertDate(const void **date1) 
    {
       ProcessControl& control = getControlArea(); 
       TPF_regs regs; 
    
       if(date1 !=0) 
       { 
          regs.r7 = *(long *) *date1; 
          control.input.location = DatWk::FIRST_R7;
       }
       UA80(Žs);
       return regs.r1;
    }

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    4
    my code was actually correct. I was doing a 'duh' on my build. it was actually my build that wasn't correct.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>:: is not the issue and works fine.
    As already said, :: is not a C operator, but it is C++. We have a seperate forum for C++ Q&A.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    4
    and it still doesn't matter as it wasn't the question. the question was concerning dereferencing which is C.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by cricket
    and it still doesn't matter as it wasn't the question. the question was concerning dereferencing which is C.
    No it isn't. Pay attention.

    :: is C++, as such, your entire program is now a C++ program. You cannot have one line be C++ and another be "just C". If you have one line C++, the compiler treats the entire program as C++. (This is simplifying a bit, and there is a way to force the issue, but in your case, it doesn't apply even remotely.)

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to better manage large .cpp files
    By 39ster in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2008, 08:24 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Error, not sure what it means.
    By RealityFusion in forum C++ Programming
    Replies: 1
    Last Post: 09-25-2005, 01:17 PM
  5. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM