简体   繁体   中英

How to print the values pointed by an array of void pointers

I have defined a structure to hold a group of values of any type, that's why the void **data. I would really appreciate if someone helps me in how to print the values pointer by an array of void pointers.

Here is the structure.

typedef struct item{
   void **data;//array of void pointer to store names of the group-members
   int count;//number of members in the group
   struct item *next; //pointer to the next group in the pool queue
} group;

Now, let say a group is created as follows:

group *gp = (group*) malloc(sizeof(group));
void *a[2];
a[0] = (void*) "John";
a[1] = (void*) "Jim";
gp->data = a;
gp->count = 2;

At some point, probably inside another function, if I need to print the array of values of a group node, what code should I write? Here is such a function:

void print_group_node(group *gp)
{
   //I have the check for vp==NULL here
   void **vp = gp->data;
   int c = gp->count;
   int i;
   for(i = 0; i < c; i++)
      printf();//what should I write here
   //other codes here
}

Thank you.

If the data could be any type of variable. You probably need one more field similar to data_type below in the structure group to tell what type data has.

typedef struct item{
   ....
   int data_type; // 0: int, 1: char * (string), ...
} group;

And use appropriate printf() as per it.

void print_group_node(group *gp)
{
   //I have the check for vp==NULL here
   void **vp = gp->data;
   int c = gp->count;
   int i;
   for(i = 0; i < c; i++)
   switch (gp->data_type) {
      case 0:
           printf("%d \n", (int*)vp[i])
           break;
      case 1:
           printf("%s \n", (char *)vp[i])
           break;
      Default:
           printf("Unknown type");
           break;
    }
}

As currently your data points to strings printf() needs "%s" to know that it needs to print string. And then typecast items in vp[] to get rid of the warning given by printf() that you are passing incompatible pointer.

 printf("%s \n", (char *)vp[i])

You cannot print values of arbitrary type in a general fashion. You could somehow store the type information and customize your print function, so that it supports all the possible types.

I suppose that this code snippet is in function main.

group *gp = (group*) malloc(sizeof(group));
void *a[2];
a[0] = (void*) "John";
a[1] = (void*) "Jim";
gp->data = a;
gp->count = 2;

and that you will access data members of the dynamically allocated structure while local array void *a[2]; is alive. Otherwise it would be better to allocate additional memory that would be pointed to by gp->data. For example

group *gp = (group*) malloc(sizeof(group));
gp->data = malloc( 2 * sizeof( void * ) );
gp->data[0] = "John";
gp->data[1] = "Jim";
gp->count = 2;

In this case that to output these data in some other function you need to know the actual type of pointer gp->data that to use an appropriate casting.

Thus if you know exactly that the actual type of data is char ** then in some other function you can write

   for(i = 0; i < c; i++)
      printf( "%s\n", ( char * ) gp->data[i] );

Otherwise you need to enumerate all types that you are going to use with your structure and to add a corresponding data member that will be used in determination of the actual type of the pointer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM