Thread: Warning:dereferencing `void *' pointer

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    85

    Warning:dereferencing `void *' pointer

    Hi again!

    In my program i have declared a function with this prototype:

    Code:
    int function(void *var) //The second argument is a void * pointer
    Also i have a struct 
    
    typedef struct hello hello;
    struct hello
    {
            char *a;
            char *b;
            int  l;
            int  t;
            struct hello *next;
    };
    
    
    hello *pointer_hello;
    When i do: function(pointer_hello);

    I get the warnings:

    warning: dereferencing `void *' pointer
    test.c:516: error: request for member `a' in something not a structure or union
    warning: dereferencing `void *' pointer
    test.c:517: error: request for member `b' in something not a structure or union
    warning: dereferencing `void *' pointer
    test.c:518: error: request for member `l' in something not a structure or union
    warning: dereferencing `void *' pointer
    test.c:519: error: request for member `t' in something not a structure or union


    What shall i change in my program?

    Well the problem is that the argument of the function "function" has to be void in order
    not only to take a pointer to a hello struct but to any other struct!

    Thanks, in advance!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's impossible to tell, since we need the actual order in which you declared things, and in which file(s) they reside.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    My guess is that you are passing a struct pointer to a function that is defined to take a void pointer. You must cast the void pointer to be that of a struct pointer before actually dereferencing it. The compiler has no idea what a void pointer is pointing to. With regard to size, it could be pointing to a byte, a word, a double word, etc. etc. There is no known way of it knowing you are passing it a struct pointer.

    In this case, you must be explicit when dealing with void *'s.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    i do
    Code:
    #define  int function( void * var ) ;
    typedef struct hello hello;
    struct hello
    {
            char *a;
            char *b;
            int  l;
            int  t;
            struct hello *next;
    };
    Then it starts
    Code:
    int main() {
    
    hello *pointer_hello;
    function(poinet_hello);
    }

    That's all!

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    function(pointer_hello);

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > #define int function( void * var ) ;
    What the hell is this supposed to mean?

    Make function accept an hello* and be done with it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    Quote Originally Posted by MacGyver View Post
    My guess is that you are passing a struct pointer to a function that is defined to take a void pointer. You must cast the void pointer to be that of a struct pointer before actually dereferencing it. The compiler has no idea what a void pointer is pointing to. With regard to size, it could be pointing to a byte, a word, a double word, etc. etc. There is no known way of it knowing you are passing it a struct pointer.

    In this case, you must be explicit when dealing with void *'s.
    How can i do this cast?
    can you make an example using the example i have given?

    Thanks, in advance!

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    struct x
    {
    	int a;
    };
    
    ...
    
    int example(void *p)
    {
    	struct x *q;
    
    	if(p)
    	{
    		q = (struct x *)p; /* Explicit cast from a void * to a struct x * */
    		q.a = 5;
    		return 1;
    	}
    	return 0;
    }
    
    ...
    
    int main(void)
    {
    	struct x v1;
    	example(&v1);
    
    	return 0;
    }
    Note: I did not check this code. I would suggest doing what was already suggested: just make your function accept a *hello.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    How can i do this cast?
    dont use void * pointer for this purpose. It looks really messy for evert struct that u wanted to refer you will have to cast it.

    It better u get the argument as struct hello * rather than void *. Which is quite more easy than void. If there are suitation some thing like where a single function need to do the same job for two different struct with same attributes but different names then may be u can think of void pointer.

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM