首页 > 解决方案 > 下标值不是指针、数组,也不是向量,只是两个基本数组,

问题描述

这是一项正在进行中的工作,所以它没有做任何事情,而且我需要清理它主要是一团糟,但是,我只是想让它达到“工作但几乎没有水平”,而我只是迷失在这个错误上,我有只有两个数组的大小设置为 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 变量,同样的错误将在上面的数组循环中抛出

标签: arrayscsyntaxlogic-error

解决方案


您在文件范围内声明了一个数组

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

使用大写字母。


推荐阅读