更改结构中的数据

时间:2018-11-20 14:17:11

标签: c struct syntax

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

#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之前。我已经搜索了Internet,但是找不到有效的方法。你能告诉我如何纠正它们吗?

2 个答案:

答案 0 :(得分: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 :(得分:1)

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

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

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

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