How to print the void * in a function and how to access the void * variable in a function?

6.5k views Asked by At

I'm trying to pass a function as argument to another function with void pointer and it doesn't work

#include "header.h"
void print ( void *Arg )
{
//  while ( ( int *) Arg[0] )
    {
        printf ( "Arg[0] = %d Arg[1] = %d\n",(int *)  Arg[0], (int * ) Arg[1] );
        sleep ( 1 );
        //Arg[0]--;
        //Arg[1]--;
    }
}
void main(int argc, char **argv)
{
    int count[2] = { 10, 160};
    print (count);
}

I Am getting errors like this:

void*.c: In function ‘print’:
void*.c:6:52: warning: dereferencing ‘void *’ pointer [enabled by default]
   printf ( "Arg[0] = %d Arg[1] = %d\n",(int *)  Arg[0], (int * ) Arg[1] );
                                                    ^
void*.c:6:3: error: invalid use of void expression
   printf ( "Arg[0] = %d Arg[1] = %d\n",(int *)  Arg[0], (int * ) Arg[1] );
   ^
void*.c:6:69: warning: dereferencing ‘void *’ pointer [enabled by default]
   printf ( "Arg[0] = %d Arg[1] = %d\n",(int *)  Arg[0], (int * ) Arg[1] );
                                                                     ^
void*.c:6:3: error: invalid use of void expression
   printf ( "Arg[0] = %d Arg[1] = %d\n",(int *)  Arg[0], (int * ) Arg[1] );

How do I fix the problem?

4

There are 4 answers

0
Some programmer dude On BEST ANSWER

To print a pointer you need the "%p" printf format.

But it seems that you don't actually want to print the actual pointer but what it points to, and this is where your error comes from, because your casting is in the wrong place, you need to cast the pointer before you dereference it, like e.g.

((int *) Arg)[0]

It's a problem with operator precedence where the array subscript operator has higher precedence than the type-cast operator. So the compiler thinks you are doing (int *) (Arg[0]).

0
Sourav Ghosh On

The array subscript operator (de-referencing) can be used with a complete type. void * is not a complete type that you can dereference.

To get this done, you need to cast the Arg itself to int * and then use the dereference operator to get the value.

Also, to print a pointer, you need to use %p format specifier.

0
Guillaume Munsch On

Try it like this maybe

printf ( "Arg[0] = %d Arg[1] = %d\n",((int *)Arg)[0], ((int * )Arg)[1] );
0
Vlad from Moscow On

The problem with the code is that the subscript operator used in the printf as for example Arg[0] or Arg[1] is applied to a pointer of type void *.

You have at first to cast this pointer to type int * and only then to apply the subscript operator

void print ( void *Arg )
{
//  while ( ( int *) Arg[0] )
    {
        printf ( "Arg[0] = %d Arg[1] = %d\n", ( (int *)  Arg )[0], ( (int * ) Arg )[1] );
        sleep ( 1 );
        //Arg[0]--;
        //Arg[1]--;
    }
}

Or the code would be simpler if in the beginning of the function you would define a local pointer. For example

void print ( void *Arg )
{
    int *p = Arg;
//  while ( ( int *) Arg[0] )
    {
        printf ( "Arg[0] = %d Arg[1] = %d\n", p[0], p[1] );
        sleep ( 1 );
        //Arg[0]--;
        //Arg[1]--;
    }
}