考虑到我有两个结构,其中一个指向另一个结构,我怎样才能从第二个结构中打印出一个vallue,为第一个结构打算?
以下节目的简要说明:
NODE,一种结构,指向itselft制作桩和通用指针 ALUNO,NODE中通用指针指向的结构 createNode,为节点分配内存的函数 preencherNode,用相应的ALUNO填充节点的指针 pushNode,创建一个新节点,指向最后创建的节点,制作一个堆 criarAluno,为ALUNO分配记忆并填补阵营。
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int id;
float media;
} ALUNO;
typedef struct node
{
void *pointer;
struct node* link;
} NODE;
NODE* createNode(void)
{
NODE* topo;
topo=(NODE*)malloc(sizeof(NODE));
topo->pointer = NULL;
topo->link = NULL;
return(topo);
}
NODE* pushNode (NODE* topo)
{
NODE* novo;
novo=createNode();
novo->link=topo;
topo = novo;
return(topo);
}
NODE* preencherNode (NODE* topo, ALUNO* data)
{
topo->pointer=data;
return(topo);
}
ALUNO* criarAluno(FILE* v)
{
ALUNO* aln;
aln=(ALUNO*)malloc(sizeof(ALUNO));
int x;
float y1,y2;
fscanf (v, "%d %f %f",&x,&y1,&y2);
aln->id=x;
y1=(y1+y2)/2;
aln->media=y1;
return(aln);
}
void printData (NODE* topo)
{
NODE* aux;
aux=topo;
ALUNO* aln;
while(aux->link!=NULL)
{
aln=((ALUNO*)(aux->link));
printf("ID: %d \n",aln->id);
printf("Media: %f \n",aln->media);
printf("........................ \n");
aux=aux->link;
}
}
void main ()
{
FILE *doc;
doc = fopen("documento.txt","r");
if (doc==NULL){
printf("Nao ha como abrir o arquivo");
return(-1);
}
NODE *pilha;
pilha=createNode();
ALUNO* aluno;
int x;
for(x=0;x<11;x++)
{
aluno=criarAluno(doc);
pilha=preencherNode(pilha,aluno);
pilha=pushNode(pilha);
}
printData(pilha);
}
现在,函数printData正在打印我想要的值的已分配空间内存,而不是值。
答案 0 :(得分:1)
如上所述,你有NODE *节点....
ALUNO *aluno;
aluno = (ALUNO *)(node->pointer)
printf("%d", aluno->id)