Thread: void pointer

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    70

    void pointer

    I was wondering why do we have a void pointer being returned when we have the function

    [code]
    void *malloc(size_t number_of_byte)

    why should it not return an integer/float/... pointer.

    Also what is the difference between null and void) i thought they were the same. i mean in
    int main(void), void means it takes in zero or null parameters, right?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    A pointer to void is C's generic pointer. Any pointer can be obtained from a pointer to void and vice versa (excepting function pointers). malloc returns a pointer to void because it can't know what type of pointer you really wanted, it only knows the byte count that you pass to it.

    >Also what is the difference between null and void
    void is a data type and null is a value. To be more precise, void is an incomplete type with an empty set of values that cannot be instantiated. Null can be confused for multiple things, but for the most part it means the value of an invalid pointer while still having a predictable and testable value.

    >i mean in int main(void), void means it takes in zero or null parameters, right?
    Yes.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. saying hello and 1st question
    By darksys in forum C Programming
    Replies: 12
    Last Post: 10-31-2008, 02:58 PM
  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. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM