void* question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts

  • =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?=
    Guest replied
    Re: void* question

    Ian Collins:
    >Same size and representation is good enough for me -- they're the same
    >thing just with a different interface to the programmer.
    >>
    Try dereferencing them and see what you get...

    I covered this in my list of differences between char* and void* upthread.

    --
    Tomás Ó hÉilidhe

    Leave a comment:


  • Ian Collins
    Guest replied
    Re: void* question

    Tomás Ó hÉilidhe wrote:
    Peter 'Shaggy' Haywood:
    >
    >Although they do have the same size and representation, this does not
    >make them the same thing, by any means.
    >
    >
    Same size and representation is good enough for me -- they're the same
    thing just with a different interface to the programmer.
    >
    Try dereferencing them and see what you get...

    --
    Ian Collins.

    Leave a comment:


  • =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?=
    Guest replied
    Re: void* question

    Peter 'Shaggy' Haywood:
    Although they do have the same size and representation, this does not
    make them the same thing, by any means.

    Same size and representation is good enough for me -- they're the same
    thing just with a different interface to the programmer.

    void * has implicit conversion to and from every pointer
    to object and incomplete type, but not pointer to function type.

    Nice catch, thanks for the correction.

    --
    Tomás Ó hÉilidhe

    Leave a comment:


  • Peter 'Shaggy' Haywood
    Guest replied
    Re: void* question

    Groovy hepcat Tomás Ó hÉilidhe was jivin' in comp.lang.c on Mon, 11 Feb
    2008 11:17 am. It's a cool scene! Dig it.
    Janet:
    >
    >What is the major reason for using void*?
    >When should we use void* for both input arguments and return value?
    >How can we cast the void* pointer to the type we need?
    >Thanx
    >
    A void* is really just a char* that has few more "features" (...or
    even "lack of features").
    Wrong, I'm afraid. A void * is not a char * at all. Although they do
    have the same size and representation, this does not make them the same
    thing, by any means.
    The differences between char* and void* are:
    1) void* has implicit conversion to and from every other pointer type.
    Wrong again. void * has implicit conversion to and from every pointer
    to object and incomplete type, but not pointer to function type.

    --
    Dig the sig!

    ----------- Peter 'Shaggy' Haywood ------------
    Ain't I'm a dawg!!

    Leave a comment:


  • Malcolm McLean
    Guest replied
    Re: void* question


    "Janet" <no@mail.comwro te in message
    On 11 Feb 2008 at 4:03, Jack Klein wrote:
    >On Sun, 10 Feb 2008 22:41:58 +0000, Flash Gordon
    >><spam@flash-gordon.me.ukwro te in comp.lang.c:
    >>
    >>Eric Sosman wrote, On 10/02/08 21:49:
    The functions all take the same arguments, but they have different
    return values, e.g.
    int f1(int x, int y);
    double f2(int x, int y);
    etc.
    >
    As I'm never actually going to need the return value I was hoping to use
    a void* instead of actual function pointers to let me ignore the
    difference between the return types of the functions. Is that going to
    work?
    >
    You need wrapper functions

    void F1(int x, int y)
    {
    gf(x, y);
    }

    void F2(int x, int y)
    {
    f2(x, y);
    }

    Now you can have a list of

    typedef void (*fptr)(int x, int y);

    fptrs (function pointers) to select the appropriate function.

    --
    Free games and programming goodies.


    Leave a comment:


  • =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?=
    Guest replied
    Re: void* question

    Janet:
    What is the major reason for using void*?
    When should we use void* for both input arguments and return value?
    How can we cast the void* pointer to the type we need?
    Thanx

    A void* is really just a char* that has few more "features" (...or even
    "lack of features").

    The differences between char* and void* are:
    1) void* has implicit conversion to and from every other pointer type.
    2) You can do pointer arithmetic on a char*, but NOT on a void*.
    3) You can dereference a char*, but not a void*.

    Basically the idea is that you use a void* when you aren't certain what
    you're going to be pointing to. You might be pointing to an int one minute,
    then a double the next.

    At the end of the day tho, both void* and char* really just store the
    address of a byte.

    --
    Tomás Ó hÉilidhe

    Leave a comment:


  • Ben Pfaff
    Guest replied
    Re: void* question

    Janet <no@mail.comwri tes:
    What is the major reason for using void*?
    A pointer to void is often used when the type of data that a
    pointer points to is unknown at compile time.
    When should we use void* for both input arguments and return value?
    When the above is true about input arguments and return values.
    How can we cast the void* pointer to the type we need?
    There is rarely a need to cast a pointer to void, because most
    conversions from a pointer to void to a pointer to another object
    type can take place implicitly, but the syntax is no different
    from that for any other cast.
    --
    "In My Egotistical Opinion, most people's C programs should be indented six
    feet downward and covered with dirt." -- Blair P. Houghton

    Leave a comment:


  • Eric Sosman
    Guest replied
    Re: void* question

    Janet wrote:
    What is the major reason for using void*?
    To deal with a pointer when the type of the thing
    it points to is unknown or unimportant. For example,
    the malloc() function allocates memory but does not
    know or need to know what kind of data you intend to
    store in it.
    When should we use void* for both input arguments and return value?
    As above.
    How can we cast the void* pointer to the type we need?
    With a cast operator, like `(int*)ptr' or `(char*)ptr'.
    Since the void* pointer carries no information about the
    kind of data it points to, you need some other way to
    figure out which cast operator is appropriate.

    --
    Eric Sosman
    esosman@ieee-dot-org.invalid

    Leave a comment:


  • Malcolm McLean
    Guest replied
    Re: void* question


    "Janet" <no@mail.comwro te in message news
    What is the major reason for using void*?
    When should we use void* for both input arguments and return value?
    How can we cast the void* pointer to the type we need?
    Thanx
    >
    Take a proposed addition to the standard library.

    void memswap(void *ptr1, void *ptr2, size_t len)

    it simply exchanges two areas of memory.
    So to swap two integers we would write

    int i, j;
    memswap(&i, &j, sizeof(int));

    to exchange two doubles

    double x, y;
    memswap(&x, &y, sizeof(double)) ;

    and so on.

    The void * parameters mean we don't need ugly casts. Internally, however,
    memswap will handle the pointers as unsigned char *s. It doesn't need to
    understand what it swaps, it just sees two areas of memory.

    --
    Free games and programming goodies.


    Leave a comment:


  • Janet
    Guest started a topic void* question

    void* question

    What is the major reason for using void*?
    When should we use void* for both input arguments and return value?
    How can we cast the void* pointer to the type we need?
    Thanx

Working...