Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to write code in C in CodeBlocks and this little section of code is

ID: 3581989 • Letter: I

Question

I am trying to write code in C in CodeBlocks and this little section of code is giving me an error " subscripted value is neither an array or a pointer". any helpful hints to fix this problem thanks.

{
int i = 0;
   Attributes *1 = (Attributes*) malloc(patients*sizeof(Attributes));
   while(i < patients && fscanf(x->inp, "%s %f %f %d %d %d %d %d",
           1[i].name, &1[i].height, &1[i].weight, &1[i].sex, &1[i].age,
           &1[i].activity_level, &1[i].sbp, &1[i].dsp) == 8)
   {
       ++i;

   }
   1[i].age=-1;
   return 1
}

Explanation / Answer

Answer

In C you can use the subscript operator [] on arrays and on pointers. When you use this subscript operator on a pointer, the resultant type is the type to which the pointer points to. Take for instance, if you apply [] to int*, the result would be an int.

when you are scanning the values, scan them and keep them in required variables and the assign it to the struct elements as below:

suppose you scanned height in variable height_sc, so assignment will be

1[i].height = (void *) height_sc;

similarly the rest of the values has to be done.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote