arrays - 下标值不是指针、数组,也不是向量,只是两个基本数组,
问题描述
这是一项正在进行中的工作,所以它没有做任何事情,而且我需要清理它主要是一团糟,但是,我只是想让它达到“工作但几乎没有水平”,而我只是迷失在这个错误上,我有只有两个数组的大小设置为 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", address);
printf("%d\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,&size,addresses) ) != EOF)
请注意,使用小写字母定义 macto 名称是一个坏主意,例如在此指令中
#define pages 256
使用大写字母。
推荐阅读
- react-native - UseEffect 正确读取值,但如果在其他地方访问,则值未定义
- php - 当我尝试在 Laravel 8“从空值创建默认对象”中更新我的博客项目中的帖子时看到此错误,这是什么意思?
- kotlin - 如果分号在 kotlin 中是可选的,kotlin 编译器如何理解语句的结尾?
- apache-beam - 我找不到包含 org.apache.beam.sdk.io.aws2.sqs.SqsIO 类的 jar
- javascript - 显示覆盖网页的 pdf 文件
- nestjs - 如何获得完整的@parent 状态(从树的多个级别)
- excel - Laravel:将excel导入数据库时出错
- drools - 具有自定义样式和自定义背景图像的 Docker 图像 (jboss/jbpm-server-full:latest)
- java - ["abcd"] 和 [["abcd"]] 是否相同?
- java - 如何为每个脚本使用 selenium 脚本代码?