问题描述
这是一项正在进行的工作,因此不会做任何事情,它主要是一个烂摊子,我需要清理,但是,即时通讯只是试图得到它的一个“工作,但几乎没有水平”,而我只是失去了对这个错误,我只有两个数组的大小设置为 16,每当我想遍历它们并执行 array[pos] 时,我都会在标题中抛出错误,我可能只是遗漏了一些非常基本的东西,但我完全迷失了
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
// global instalization (to be edited?)
#define pages 256
#define page_size 256
#define memory_size pages * page_size
#define TLB_SIZE 16
int page_table[pages];
int TLB[TLB_SIZE];
int TLB_frame[TLB_SIZE];
int main(int argc,char *argv[])
{
// BASIC INTIALIZATION FOR PAGE AND OFFSET,AND FILE,ADDRESS
char *address;
int page = 0;
int offset = 0;
size_t size = 0; // filler variable;
int eof=0; // end of file;
FILE* addresses;
addresses = fopen(argv[1],"r");
while (eof = getline(&address,&size,addresses) != EOF)
{
int TLB_frame = 0;
int frame = 0;
int pos;
page = atoi(address) / 256 ;
offset = atoi(address) % 256;
printf("here is the page number for %s\n",address);
printf("%d\n",page);
printf("here is the offset for %s\n",offset);
for (pos = 0; pos < TLB_SIZE; pos++)
{
if(TLB[pos] == page)
{
frame = TLB_frame[pos];
}
}
}
}
同样,这并没有做任何事情,它只是一个正在进行的工作,可能还有很多不必要的事情
error: subscripted value is neither array nor pointer nor vector
46 | frame = TLB_frame[pos];
如果我弄乱了 pos 变量,同样的错误将在上面的数组循环中抛出
解决方法
您在文件范围内声明了一个数组
int TLB_frame[TLB_SIZE];
然后在while循环中
while (eof = getline(&address,&size,addresses) != EOF)
{
int TLB_frame = 0;
//...
您重新声明了名称 TLB_frame
。所以在这个块范围内,名称 TLB_frame
并不表示在文件范围内声明的数组。它是 int
类型的标量对象。
似乎while语句中的条件应该是
while ( ( eof = getline(&address,addresses) ) != EOF)
注意使用小写字母定义macto名称是一个坏主意,例如在这个指令中
#define pages 256
使用大写字母。