更改struct中的数据

问题描述 投票:4回答:2

我尝试编写一个函数来更改结构中的数据。这是我的代码的一部分。

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<unistd.h>
#include<pthread.h>
#include<sys/types.h>
#define THREADNUM 20 

pthread_mutex_t DeviceMutex ;
struct VirtualPCB
{
    int tid;
    int handlePriority;
    int arrivetime;
    int waittime;
    int runtime;
    int visited;
    int tempruntime;
    int finishtime;
}PCBs[THREADNUM];


void initPCB()
{
    int n;
    srand(time(NULL));
    for(n =0;n<THREADNUM;n++)
    {

        PCBs[n].tid = n + 1;
        PCBs[n].handlePriority = 1 + rand()%19;
        PCBs[n].arrivetime = 1 + rand()%19;
        PCBs[n].tempruntime=PCBs[n].runtime = 1 + rand()%19;
        PCBs[n].waittime = 0;
        PCBs[n].visited =0;
        PCBs[n].finishtime = PCBs[n].arrivetime + PCBs[n].runtime;
    }
}

void change(PCBs[THREADNUM],int i, int j)
{
    int temp;
    temp = PCBs[i].arrivetime;
    PCBs[i].arrivetime = PCBs[j].arrivetime;
    PCBs[j].arrivetime = temp;
    temp = PCBs[i].runtime;
    PCBs[i].runtime = PCBs[j].runtime;
    PCBs[j].runtime = temp;
    temp = PCBs[i].finishtime;
    PCBs[i].finishtime = PCBs[j].finishtime;
}

但是有一个错误。

“错误:预期声明说明符或'...'

PCBs之前。我搜索过互联网,但找不到有效的方法。你能告诉我如何纠正它们吗?

c struct syntax
2个回答
2
投票

函数定义的语法错误。你需要改变

  void change(PCBs[THREADNUM],int i, int j) { ....

  void change(struct VirtualPCB PCBs[THREADNUM],int i, int j) { ...

要么

void change(struct VirtualPCB PCBs[ ],int i, int j) {....

1
投票

这不是定义函数的有效语法:

void change(PCBs[THREADNUM],int i, int j)

第一个参数需要您未指定的类型:

void change(struct VirtualPCB PCBs[THREADNUM],int i, int j)
© www.soinside.com 2019 - 2024. All rights reserved.