Thread: Function returns pointer of unknown type..

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Function returns pointer of unknown type..

    Hi,

    If i am to have a function to return a pointer of an unknown type, is this the right prototype for it?

    Code:
    void *function( void *one, void *two );

  2. #2
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Yes. You are simply returning an address without any indication as to what type of data the address refers to.
    Jason Deckard

  3. #3
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    but it can still be used right?

  4. #4
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    Probably with something like this.

    char *s = NULL;
    char *blah1, *blah2;

    s = function(blah1, blah2);

  5. #5
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Originally posted by dharh
    Probably with something like this.

    char *s = NULL;
    char *blah1, *blah2;

    s = function(blah1, blah2);
    Something like that :) You will want to use a typecast since function() is returning a NULL pointer:

    Code:
    s = (char *) function(blah1, blah2);
    Jason Deckard

  6. #6
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    Im not sure thats necessary if you protoype it as a pointer.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > Something like that You will want to use a typecast since
    > function() is returning a NULL pointer:

    Slight correction here. It's not returning a NULL pointer, it's returning a VOID pointer. Null is a value of zero, meaning it points to nothing. A void pointer is a pointer who's type can be anything. Typecasting is not usually required in C when using void pointers. (Consider 'malloc'.)

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

  8. #8
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Originally posted by quzah
    > Something like that You will want to use a typecast since
    > function() is returning a NULL pointer:

    Slight correction here.
    You are absolutely correct. Simply a typographical error on my part.
    Jason Deckard

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Typecasting a void * is actually -never- required in C... but C++
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM