Results 1 to 14 of 14

Thread: void pointer in C Programming Language

  1. #1
    Shivanna
    Guest

    void pointer in C Programming Language

    What is the use of void pointer? When and Why do we make use of void pointers in C programming Language?

    Regards
    Shivanna


  2. #2
    Contributing Member
    Join Date
    May 2006
    Answers
    64

    Re: void pointer in C Programming Language

    void pointers can be assigned to any pointer value. It sometimes necessary to store/copy/move pointers without regard to the type it references.
    You cannot dereference a void pointer.
    Functions such as malloc, free, and scanf utilize void pointers.
    So one must be careful while using void pointer


  3. #3
    Junior Member
    Join Date
    Nov 2006
    Answers
    3

    Re: void pointer in C Programming Language

    how can i use void pointer?can u give examples?
    my program gives error like this.
    error C2036: 'void *' : unknown size
    error C2440: 'static_cast' : cannot convert from 'void' to 'int'


  4. #4
    Contributing Member
    Join Date
    May 2006
    Answers
    82

    Re: void pointer in C Programming Language

    Void is a non-type which has no size, no format, it is a black hole from which you cannot read. void * is pointer-to-void. You would have assigned or read or retrieved some values pointing to nothing. Only if your post your code I could give a more elaborate and exact answer.


  5. #5
    Expert Member
    Join Date
    Dec 2006
    Answers
    204

    Re: void pointer in C Programming Language

    You can't compare pointers of different types. eg. You cant compare an int and char pointer.
    In this case, you can declare one pointer as void and do comparision.


  6. #6
    Junior Member
    Join Date
    Nov 2006
    Answers
    3

    Re: void pointer in C Programming Language

    thanks for your attendance.
    i want to use void * like templates(to do my homework).
    first i have to assign void * to char *.then compare them byte to byte.
    but i couldn't do this.i don't know its syntax.
    can you know how to do this?


  7. #7
    Junior Member
    Join Date
    Nov 2006
    Answers
    3

    Re: void pointer in C Programming Language

    this is my hw.

    Attached Files Attached Files
    Last edited by kingalmora; 12-23-2006 at 11:03 AM.

  8. #8
    Junior Member
    Join Date
    Sep 2006
    Answers
    23

    Re: void pointer in C Programming Language

    Friends is void pointer different from null pointer if so what is the difference.Can someone explain with some example.


  9. #9
    Junior Member
    Join Date
    Jan 2007
    Answers
    9

    Re: void pointer in C Programming Language

    Quote Originally Posted by JamesMike View Post
    Friends is void pointer different from null pointer if so what is the difference.Can someone explain with some example.
    A void pointer is a pointer which can point to any data type (which of course requires typecasting). Where as a null pointer is used to indicate that it is pointing nowhere.

    A null pointer is a value that any pointer may take to represent that it is pointing to "nowhere", while a void pointer is a special type of pointer that can point to somewhere without a specific type. One refers to the value stored in the pointer itself and the other to the type of data it points to.


  10. #10
    Junior Member
    Join Date
    Feb 2007
    Answers
    1

    Re: void pointer in C Programming Language

    Just to add that we cannot do pointer arithmetic on Void Pointers.


  11. #11
    Junior Member
    Join Date
    Jun 2006
    Answers
    2

    Lightbulb Re: void pointer in C Programming Language

    I think void pointer is used in 'vtable' inorder to avoid the confilct of the linker in run time binding.


  12. #12
    Junior Member
    Join Date
    Mar 2007
    Answers
    1

    Re: void pointer in C Programming Language

    Hi, void pointer is a type of pointer for what u can decide them to poin what u want.


  13. #13
    Junior Member
    Join Date
    Jul 2008
    Answers
    1

    Re: void pointer in C Programming Language

    Quote Originally Posted by janelyn View Post
    void pointers can be assigned to any pointer value. It sometimes necessary to store/copy/move pointers without regard to the type it references.
    You cannot dereference a void pointer.
    Functions such as malloc, free, and scanf utilize void pointers.
    So one must be careful while using void pointer
    Hi, you can assign other pointer types(including void pointer) to a void pointer without an explicit cast(converse is not true though).
    If not directly, a void pointer can be dereferenced indirectly by casting it to the appropriate type. For ex:

    int val = 6;
    void *v;

    v= &val;
    printf("%d", *(int *)v); // would give you the output as 6.

    char *s = "abc";
    void *v2;

    v2= s;
    printf("%s", (char *)v2); // would give you the output as "abc".


  14. #14
    Junior Member
    Join Date
    Feb 2009
    Answers
    3

    Re: void pointer in C Programming Language

    hey first of all every buddy need to understand the need of defining the type of any pointer


    defining the type of any pointer tells compiler how much memory(how many bytes) to be accessed while appling some operation to it.
    eg.
    int var = 10;
    int *ptr = &var;


    now compiler knows if value is to be read through ptr then 4 bytes to be read and tell.


    now in case of void type of pointer compiler never knows how many bytes to be accessed.
    so inorder access it we need to inform compiler about its type or how many bytes to be accessed.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact