L.Ying
L.Ying

Reputation: 59

change data in struct

I try to write a function to change data in a struct. Here are part of my code.

#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;
}

but there is an error.

"error:expected declaration specifier or '...'

before PCBs. I have searched the Internet, but I can not find an effective way. Can you tell me how to correct them?

Upvotes: 4

Views: 50

Answers (2)

Sourav Ghosh
Sourav Ghosh

Reputation: 134396

The syntax for function definition is wrong. You need to change

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

to

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

or

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

Upvotes: 2

dbush
dbush

Reputation: 225344

This is not valid syntax for defining a function:

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

The first parameter need a type which you didn't specify:

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

Upvotes: 1

Related Questions