[英]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. 我已经定义了一个结构来保存任何类型的一组值,这就是void **数据的原因。 I would really appreciate if someone helps me in how to print the values pointer by an array of void pointers.
如果有人帮助我如何通过void指针数组打印值指针,我真的很感激。
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. 您可能需要在结构
group
再添加一个类似于data_type
字段来说明data
类型。
typedef struct item{
....
int data_type; // 0: int, 1: char * (string), ...
} group;
And use appropriate printf()
as per it. 并根据它使用适当的
printf()
。
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. 目前您的数据指向字符串
printf()
需要"%s"
才能知道它需要打印字符串。 And then typecast items in vp[] to get rid of the warning given by printf()
that you are passing incompatible pointer. 然后用vp []中的类型转换来摆脱
printf()
给出的警告,告知你传递了不兼容的指针。
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. 您可以以某种方式存储类型信息并自定义您的
print
功能,以便它支持所有可能的类型。
I suppose that this code snippet is in function main. 我想这个代码片段在函数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];
并且您将访问动态分配的结构的数据成员,而本地数组
void *a[2];
is alive. 活着。 Otherwise it would be better to allocate additional memory that would be pointed to by gp->data.
否则,最好分配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. 在这种情况下,要在其他函数中输出这些数据,您需要知道使用适当的转换的指针gp-> data的实际类型。
Thus if you know exactly that the actual type of data is char **
then in some other function you can write 因此,如果您确切地知道实际的数据类型是
char **
那么在其他一些函数中您可以编写
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. 否则,您需要枚举要与结构一起使用的所有类型,并添加将用于确定指针实际类型的相应数据成员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.