Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

something not a structure or union 1

Status
Not open for further replies.

blues77

Programmer
Jun 11, 2002
230
0
0
CA
i'm getting this error when i run the following code

robin.c: In function `runProcess':
robin.c:72: request for member `name' in something not a structure or union
robin.c:73: request for member `executionTime' in something not a structure or union

#include <stdio.h>
#include <stdlib.h>
#define NAME 10

typedef struct
{
char name[NAME];
int timeForExecution;
int timeLeft;
int waitTime;
int turnAroundTime;
} Process;


int main(int argc, char* argv[])
{
Process* p;
char name[NAME];
int numberOfProcesses = 0;
int timeSlice = 0;
int i = 0;
int execTime;

printf(&quot;Please enter the number of processes\n&quot;);
scanf(&quot;%d&quot;, &numberOfProcesses);
printf(&quot;Please enter the timeslice\n&quot;);
scanf(&quot;%d&quot;,&timeSlice);

//allocate memory
p = malloc(numberOfProcesses*sizeof(Process));

for(i = 0; i < numberOfProcesses; i++)
{
printf(&quot;Please enter a name for this process\n&quot;);
scanf(&quot;%s&quot;, name);
strcpy(p.name,name);

printf(&quot;Please enter the ammont of time this process requires to complete execution\n&quot;);
scanf(&quot;%d&quot;, &execTime);
p.timeForExecution = execTime;
}

roundRobin(p, numberOfProcesses, timeSlice);
}

roundRobin(Process* p, int numberOfProcesses, int timeSlice)
{
int totalExecutionTime = 0;
int i = 0;

for(i = 0; i < numberOfProcesses; i++)
{
totalExecutionTime = totalExecutionTime + p.timeForExecution;
}

while(totalExecutionTime > 0)
{
for(i = 0; i < numberOfProcesses; i++)
{
runProcess(&p, &totalExecutionTime);
}
}
}

runProcess(Process* p, int* totalTime)
{
printf(&quot;Name is %s&quot;, p.name);
printf(&quot;exec time is %d&quot;, p.executionTime);
}
 
This error is due to accessing a structure pointer member variable with &quot;.&quot; operator.

Use &quot;->&quot; operator.

-Latha
 
Hi Blues,

I am not sure what Latha has suggested is right or not. Since if you go by that suggestion, then there has to be lot error messages bcos in the code everywhere you are using &quot;.&quot; instead of &quot;->&quot;

My doubt is while calling the function runProcess why do u need to send the address of P, i.e., &p in the function roundRobin (For UR Ref: runProcess(&p, &totalExecutionTime);).

So p in the runprocess function will become a pointer to a structure pointer. So when u try to dereference it then you will definitely have a problem.

Hope i made myself very clear.

One more clarification.
When i went thru the entire code i could see that &quot;executionTime&quot; is not a member of the structure.
Kindly look into this also.

Pls let me know the result

Thanks
Karthik
 
Karthiks,
Latha is correct. When you have a pointer to a structure, you should use -> instead of . as you do if you have a structure. //Daniel
 
The line

strcpy(p.name,name);

should read

strcpy(p.name,name);

otherwise you will be continually overwriting the first entry.
 
If you use without switching off TGML, it won't appear, and the remaining script ends up in italics.
HTH ;-)
Dickie Bird
Honi soit qui mal y pense
 
I'm not sure what you are doing here. It looks like you are trying to alloc space
for an array of pointers to type Process,
but then you go through your build loop without incrementing the pointer or even declaring the array correctly...


Here it is without any of the questionable stuff, it compiles and runs but probably is not what you wanted.

#include <stdio.h>
#include <stdlib.h>
#define NAME 10

typedef struct
{
char name[NAME];
int timeForExecution;
int timeLeft;
int waitTime;
int turnAroundTime;
} Process;


int main(int argc, char* argv[])
{
Process *p;
int numberOfProcesses;
int timeSlice;
int i;

printf(&quot;Please enter the number of processes\n&quot;);
scanf(&quot;%d&quot;, &numberOfProcesses);
printf(&quot;Please enter the timeslice\n&quot;);
scanf(&quot;%d&quot;,&timeSlice);

//allocate memory
p = malloc(sizeof(Process));


printf(&quot;Please enter a name for this process\n&quot;);
scanf(&quot;%s&quot;, p->name);


printf(&quot;Please enter the ammont of time this process requires to complete execution\n&quot;);
scanf(&quot;%d&quot;, &p->timeForExecution);

roundRobin(p, numberOfProcesses, timeSlice);
return 0;
}

roundRobin(Process* p, int numberOfProcesses, int timeSlice)
{
int totalExecutionTime = 0;
int i = 0;

totalExecutionTime = totalExecutionTime + p->timeForExecution;


while(totalExecutionTime > 0)
{
for(i = 0; i < numberOfProcesses; i++)
{
runProcess(p, totalExecutionTime);
totalExecutionTime--;
}
}
}

runProcess(Process* p, int totalTime)
{
printf(&quot;\nName is %s&quot;, p->name);
printf(&quot;\nexec time is %d&quot;, p->timeForExecution);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top