C字符串和位运算符

C字符串和位运算符,c,arrays,bitwise-operators,C,Arrays,Bitwise Operators,因此,我有一个输入文件,其中包含: 1 2 288815 1 13 60980 1625768418250730 02468583061388 162103564 0 0 358489 1 13 219326 0 0 9676 0 3 402661 0 3 280447 0 3 288153 1749570397725 此信息已逐行读取到数组中 每行的第一个和第四个数字是有效的支票,第二个和第五个数字是标签,第三个和第六个数字是地址 我需要创建一个包含三个参数的函数:myArray、index和

因此,我有一个输入文件,其中包含:

1 2 288815 1 13 60980

1625768418250730

02468583061388

162103564

0 0 358489 1 13 219326

0 0 9676 0 3 402661

0 3 280447 0 3 288153

1749570397725

此信息已逐行读取到数组中

每行的第一个和第四个数字是有效的支票,第二个和第五个数字是标签,第三个和第六个数字是地址

我需要创建一个包含三个参数的函数:myArray、index和tag

该函数将检查参数中给定索引处的行。它将首先检查行中是否有与参数中的标记相等的标记。如果是,它将检查有效数字是1还是0。如果是1,则应返回标记后的地址。否则,它应该返回“页面错误”

这是我第一次尝试:

char *lookUpTLB(char **array, int TLBI, int TLBT)
{
if (array[TLBI][2] == TLBT)// checks if the second number in the array is equal to the tag
{
    //char **ar = array[TLBI] + 2;
    if (array[TLBI][0] == '1')//checks the valid number
    {
        return array[TLBI]+11;
    }
    else
    {
        return "Page Fault";
    }
}
else if (array[TLBI][13] == TLBT)//checks if the 13th index is equal to the tag
{
    if (array[TLBI][11] == '1')//checks the valid number
    {
        return array[TLBI] + 15;
    }
    else
    {
        return "Page Fault";
    }
}
else
{
    return "Page Fault";
}
}
这段代码没有什么问题,但主要思想是存在的。我不能像以前一样在代码中使用索引,因为并非所有数字的长度都相同。有人告诉我可以使用位运算符来实现这一点,但我对如何实现这一点感到非常困惑

下面是我想做的一个例子:

调用lookUpTLB(myArray,0,2);应返回288815

调用lookUpTLB(myArray,0,13);应返回60980

调用lookUpTLB(myArray,3,3);应返回页面错误,因为有效数字为0

调用lookUpTLB(myArray,3,6);应返回210352


我是C语言的新手,关于如何实现这个功能有什么帮助吗?如果我需要更清楚的话,请告诉我。

,似乎他应该转换成一个数字,如果不是固定长度,只需格式化文件即可

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>

typedef struct data {
    char valid;
    int tag;
    int32_t address;
} Data;

typedef struct rec {
    Data data[2];
} Record;

int32_t lookUpTLB(Record *array, int index, int tag);

int main(void){
    Record array[16];
    int n=0;//number of record
    FILE *fp = fopen("data.txt", "r");
    while(6==fscanf(fp, " %c %d %" SCNd32 " %c %d %" SCNd32 ,
        &array[n].data[0].valid, &array[n].data[0].tag, &array[n].data[0].address,
        &array[n].data[1].valid, &array[n].data[1].tag, &array[n].data[1].address))
    {
        ++n;
    }
    fclose(fp);
    int index, tag;
    int32_t address;
    while(1){
        printf("index(-1.:end) and tag\n");
        if(2!=scanf("%d %d", &index, &tag) || index == -1)
            break;
        if(0>(address = lookUpTLB(array, index, tag)))
            printf("Page fault\n");
        else
            printf("%" PRId32 "\n", address);
    }
    return 0;
}

#define PAGE_FAULT -1

int32_t lookUpTLB(Record *array, int TLBI, int TLBT){
    if(array[TLBI].data[0].tag == TLBT){
        return array[TLBI].data[0].valid == '1' ?
            array[TLBI].data[0].address :
            PAGE_FAULT;
    } else if(array[TLBI].data[1].tag == TLBT){
        return array[TLBI].data[1].valid == '1' ?
            array[TLBI].data[1].address :
            PAGE_FAULT;
    }
    return PAGE_FAULT;
}

由于您的所有值都是整数,因此首先解析数据更有意义,这样您就拥有了一个整数数组,而不是字符串数组。如果这个问题得到了回答,请保持原始值可见,以便以后访问站点的访问者可以看到它。
index(-1.:end) and tag
0 2
288815
index(-1.:end) and tag
0 13
60980
index(-1.:end) and tag
3 3
Page fault
index(-1.:end) and tag
3 6
210352
index(-1.:end) and tag
-1.