Thread: Passing a double to function with void *

  1. #1
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343

    Passing a double to function with void *

    Hi all, this could be a really stupid question here... but here it goes.

    I am trying to pass the address of the variable dNum to a function that takes a parameter of type void *. But nothing prints.. Any ideas?
    Code:
    #include <stdio.h> 
    
    void output(void *);
    
    int main(void)
    {
    	double dNum;
    
    	dNum = 23.2;
    	
    	output(&dNum);
    
    	return 0;
    }
    
    void output(void *pData)
    {
    	printf("%f", pData);
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    When you pass a variable as a void * don't forget to convert it back before using it. And to retrieve the data that a pointer points to you need to dereference it.
    Code:
    #include <stdio.h> 
    
    void output(void *pData)
    {
      printf("%.1f", *(double *)pData);
    }
    
    int main(void)
    {
      double dNum = 23.2;
      output(&dNum);
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Prelude - Your a legend!

    It was so straight forward, but it solved a really big problem I was having with quite a large program. I was overcomplicating the issue essentially.

    Thanks!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I was overcomplicating the issue essentially
    You'd be surprised how many people do this.

    -Prelude
    My best code is written with the delete key.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You weren't over complicating the issue, you simply forgot to type cast your void pointer when you dereferenced it. Actually, you forgot to dereference it when you went to use it with printf. That was your only mistake.

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

  6. #6
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    No, I mean I was overcomplicating the issue in the REAL program. What I have here is just a cut down version of what I thought the problem was.. which it isn't. The problem here was syntax related but was giving similar results.

    It just (through some rather bizzare lateral thinking) highlighted the issue I was having with the rest of the program.

    But yeah - there were some pretty fundamental errors in this code snippet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. How to better manage large .cpp files
    By 39ster in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2008, 08:24 AM
  3. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM