gpt4 book ai didi

C正确访问数组

转载 作者:太空宇宙 更新时间:2023-11-04 06:03:03 25 4
gpt4 key购买 nike

操作系统模拟.h

typedef enum {
PROCESS_NEW = 0,
PROCESS_READY,
PROCESS_RUNNING,
PROCESS_WAITING,
PROCESS_TERMINATED
} process_state_t;

typedef struct _pcb_t {
const unsigned int pid;
const char *name;
const unsigned int static_priority;
process_state_t state; <<---Trying to access this
op_t *pc;
struct _pcb_t *next;
} pcb_t;

文件1.c

static pcb_t **current;

extern void yield(unsigned int cpu_id)
{
/* FIX ME */
while (1)
{
pthread_mutex_lock(&current_mutex);
current[cpu_id].state = PROCESS_WAITING; ///<-------ERROR HERE
pthread_mutex_unlock(&current_mutex);
break;
}
schedule(cpu_id);
}

in main method():
current = malloc(sizeof(pcb_t*) * 10);

我在这一行 current[cpu_id].state = PROCESS_WAITING; 中有错误

error: request for member ‘state’ in something not a structure or union

这个错误是什么意思?
这不是访问包含 pcb_t 的当前数组的正确方法吗?
如果是这样,我如何访问当前数组?和状态字段?

最佳答案

您可能正在寻找:

current[cpu_id]->state = PROCESS_WAITING;

current的类型是pcb_t **。所以current[cpu_id]的类型是pcb_t *

关于C正确访问数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15752641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com