gpt4 book ai didi

c - 为什么说 vp 和 pf 都在结构节点中时未声明?

转载 作者:行者123 更新时间:2023-11-30 16:02:16 27 4
gpt4 key购买 nike

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int mms;

int ps;

int rp;

struct node{

int *pf;
int *vp;
} *pt=NULL;

void enter_params(){
printf("Enter main menmory size(words):_\n");
scanf("%d",&mms);
printf("Enter page size (word/page):_\n");
scanf("%d",&ps);
printf("Enter replacement policy(0=LRU, 1=FIFO):_\n");
scanf("%d",&rp);
}

void mapVirtualAddress(){
int VA;
int offset;
int tag;
int numEntries;
int k;
int pa;
int i;
i=0;

printf("Enter virtual memory address to access:_\n");
scanf("%d",&VA);
tag = VA/ps;
offset = VA%ps;

while((i<numEntries)&&(pt[i].vp!=vp)&&(pt[i].vp!=-1))
i++;
if(i==numEntries){
pf = pt[0].pf;
for(k=0; k<numEntries-2; i++)
pt[k] = pt[k+1];
pt[numEntries-1].vp = vp;
pt[numEntries-1]pf = pf;
printf("Page fault!");
}
else if(pt[i].vp==-1){
pt[i].vp = vp;
pt[i].pf = i;
printf("Page fault!");
}
else{
pa = pt[i].pf*ps+offset;
}
if(rp == 1){
printf("Do nothing");
}
else{
while((k<numEntries-1)&&(pt[i+1].vp!=-1)){
pt[k] = pt[k+1];
k++;
}
}
}

void printPT(){
int i;
pt[i].vp;
pt[i].pf;
while((pt[i].vp!=-1)&&(i<pt[i].vp)){
printf("VP %d --> PF %d;",vp,pf);
i++;
}
}

最佳答案

如果您寻求帮助,请尽可能轻松地让别人帮助您。您可以在其中添加评论。

现在给出答案:它们都在结构中声明。您不能直接使用 pf 或 vp,您必须声明结构体的变量,然后通过结构体使用它们。

在您的示例中,通过 pt->pfpt->vp (当然,一旦您分配了 pt。

这里pf = pt[0].pf; pf 没有声明,编译器会告诉您这一点。

此外,numEntries 从未被分配,因此您的代码会产生未定义的结果。

请发布可运行的代码,否则很难帮助您!

马里奥

关于c - 为什么说 vp 和 pf 都在结构节点中时未声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5534640/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com