【问题标题】:Why does it say that vp and pf are undeclared when they're both in the struct node?为什么说 vp 和 pf 都在结构节点中时未声明?
【发布时间】:2011-04-04 04:42:47
【问题描述】:
#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++;
        }
}

【问题讨论】:

    标签: c undeclared-identifier


    【解决方案1】:

    如果您正在寻求帮助,请尽可能让某人帮助您。 您可以在它说的地方添加评论。

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

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

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

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

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

    马里奥

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多