I\'m getting this error... // error: incompatible types when assigning to type \
ID: 3695060 • Letter: I
Question
I'm getting this error... // error: incompatible types when assigning to type 'char[5]' from type 'char *'
Here is the code
#include<stdio.h>
#include<string.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum,index = 0;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
char type[5];
char str[50];
char *ptr;
FILE *fp = fopen("input.txt", "r");
fgets(str, 100, fp); // reading line
ptr = strtok(str, " "); // splitting by space
int i=0;
while(ptr != NULL)
{
if(index == 0){
type = ptr; // error: incompatible types when assigning to type 'char[5]' from type 'char *'
index++;
}
else if(index == 1){
n = ptr; // warning: assignment makes integer from pointer without a cast
remain = n;
index++;
}
else{
at[i] = (int) strtol(ptr[1], (char **)NULL, 10);
bt[i] = (int) strtol(ptr[2], (char **)NULL, 10);
rt[i] = bt[i];
i++;
}
ptr = strtok(NULL, " "); // and keep splitting
}
fclose(fp);
char c[1000];
FILE *fptr;
fptr=fopen("output.txt","w");
fprintf(fptr,"%s"," Process |Turnaround Time|Waiting Time ");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
fprintf(fptr,"P[%d] | %d | %d ",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
fprintf(fptr," Average Waiting Time= %f ",wait_time*1.0/n);
fprintf(fptr,"Avg Turnaround Time = %f",turnaround_time*1.0/n);
return 0;
}
Explanation / Answer
You can't fix it without changing its type.
Array values are not modifiable l-values. If you plan to always assign it a string literal, declare it as
const char * instead of array.
If you want to later change the contents of types, then keep it as an array and just use strcpy() or the safer variant strncpy().
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.