#include<stdio.h>
#include<stdlib.h>
struct genericStorage
{
void *data;
int type;
};
struct genericNode
{
struct genericStorage storage;
struct genericNode *next;
};
struct genericNode *new,*temp;
struct genericNode* createGLL(int type)
{
int a;
char b;
if(type==0)
{
new=(struct genericNode *) malloc (sizeof(struct genericNode));
(new->storage).data=(int *) malloc (sizeof(int));
printf("Enter Integer");
scanf("%d",&a);
*((int *)(new->storage.data))=a;
new->storage.type=type;
new->next=NULL;
}
else if(type==1)
{
new=(struct genericNode *) malloc (sizeof(struct genericNode));
(new->storage).data=(char *) malloc (sizeof(char));
printf("Enter Char");
scanf("%c",&b);
*((char *)(new->storage.data))='c';
new->storage.type=type;
new->next=NULL;
}
else if(type==2)
{
new=(struct genericNode *) malloc (sizeof(struct genericNode));
temp=(struct genericNode *) malloc (sizeof(struct genericNode));
temp=createGLL(1);
(new->storage).data=(struct genericNode *)temp;
new->storage.type=type;
new->next=NULL;
}
}
void print(struct genericNode *t)
{
if(t->storage.type==0)
printf("%d\n",*(int *)t->storage.data);
if(t->storage.type==1)
printf("%c\n",*(char *)t->storage.data);
if(t->storage.type==2)
printf("%c\n",*(struct genericNode *)(t->storage.data->(temp->storage).data));
}
int main()
{
int type;
struct genericNode *head;
head=NULL;
printf("Enter the type");
scanf("%d",&type);
getchar();
head=createGLL(type);
print(head);
return 0;
}在打印函数中,我遇到了一个问题,我必须打印存储在外部结构中的结构中的数据。如何访问内部结构中的数据并打印出来?请紧急提供帮助。
发布于 2014-01-25 12:22:27
(t->storage.data->(temp->.data))
这句话说不通。您正在尝试访问storage.data中的一个成员,但是您可以访问temp中的一个成员,而不是一个有效成员的名称。根本不是C。
如果我理解您的代码,t->storage.data可能是指向genericNode的另一个指针。在这种情况下,您可能希望这样做:
if(t->storage.type == 2) {
struct genericNode *node = t->storage.data;
printf("Jumping to the inner genericNode at %p\n", (void *)node);
print(node); /* Call print again. */
}https://stackoverflow.com/questions/21350353
复制相似问题