Thread: printf a int/float pointer

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    72

    printf a int/float pointer

    Hi

    I have this function which returns a void pointer:

    Code:
    printf( "value is %d\n", (int*)getSomething(input) ) ;
    printf( "value is %f\n", (float*)getSomething(input1) ) ;
    This doesn't work because %d and %f do not expect a pointer. So I changed the code to

    Code:
    printf( "value is %d\n", *(int*)getSomething(input) ) ;
    printf( "value is %f\n", *(float*)getSomething(input1) ) ;
    When I execute the code I get an Bus Error. Why doesn't this work and what is the solution ?

    thnx a lot!
    Luca

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    If you want to print the address of the void pointer just print the return value of the function and use %p for formatting.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    72
    I need the value

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well, you cannot dereference a void*. What you can do is cast it to an int* for example (if you are trying to get the int value pointed at) and dereference that.

    Haven't tried this in a while so I don't remember if it works, but try it out.

    Code:
    printf("%d", (*(int*)(something(input))));

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You can't dereference void pointers since they have no size. Change your function to return the type that you are actually returning.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Just tried my code above and it works for me.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    72
    it works for me too

    But, can someone explain the differences between:

    Code:
    printf( "value is %d\n", *(int*)getSomething(input) ) ;
    ,
    Code:
    printf( "value is %d\n", *((int*)getSomething(input)) ) ;
    and
    Code:
    printf( "value is %d\n", (*(int*)getSomething(input)) ) ;
    To me, these 3 might all dereference the int value!

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by jeanluca View Post
    To me, these 3 might all dereference the int value!
    That's because the three are exactly the same....

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    72
    Thats a good thing (I thought I had Bus errors with the other 2)

    thnx!!

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by jeanluca View Post
    Thats a good thing (I thought I had Bus errors with the other 2)

    thnx!!
    Actually, that might very well be a bad thing. Because it probably means you have another unfixed flaw in there now.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    EVOEx is correct. Perhaps your problem was in whatever you were doing in that function and not in printing its return value.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    72
    I see the 3 running perfectly right now, so the bus error was indeed something else at that time!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. menu problem!!! need U R G E N T help!!!
    By catcat28 in forum C Programming
    Replies: 16
    Last Post: 11-19-2007, 01:32 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Whats Wrong Whith This!?
    By SmokingMonkey in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2003, 09:42 PM
  4. Replies: 4
    Last Post: 04-01-2003, 12:49 AM
  5. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 06:59 PM