Home>

I'm studying c language and I get an error about this problem and it doesn't work. I would appreciate it if you could teach me how to solve it.


-Define the structure with the main function.
-Pass the address of the structure "animals" as an argument to the function "showFastestAnimal".
-Do not use the subscript operator "[]" in the function "showFastestAnimal".
-Use the dot operator "." And the indirect operator "*" instead of the arrow operator "->".

<>
Fastest Resident: Natalie
Speed: 70

#include<stdio.h>

// resident: Resident information
typedef struct resident
{
    int age;// age
    char species [100];// type
    char name [100];// name
    int weight;// weight
    int speed;// speed (speed)
} JYUNIN;
void showFastestAnimal (JYUNIN * p)
{
    int i;
    int maxspeed, maxname;
    sped = (* p) .speed;
    for (i = 0;i<5;i ++)
    {
        if (sped<* (p + i) .speed)
        {
            sped = * (p + i) .speed;
            mname = * (p + i) .name;
        }
    }
    printf ("Fastest Resident:% s \ n", maxname);
    printf ("speed:% d", maxspeed);
}
int main (void)
{
    // Declare the structure with the main function
    JYUNIN animals [] = {
        {21, "Dog", "Takeke", 15, 50},
        {18, "Deer", "Natalie", 80, 70},
        {7, "Cogma", "Patch", 20, 5},
        {42, "dog", "lucky", 15, 7},
        {10, "sheep", "chacha", 25, 10}};
    // pass the address of the structure as an argument
    showFastestAnimal (animals);
    return 0;
}
The expression requires a structure or union type. I will come out.
c
  • Answer # 1

    In addition to autumn_nsn's answer
    sped = (* (p + i)). speed;
    mname = (* (p + i)). name;
    If you don't add another parenthesis like
    The structs. And->have high priority, so they are tied before the *.

    I'm not sure if it's mname or maxname,
    I think this is char *, not int.

    I haven't seen the others.
    I feel like there was a full-width space somewhere. .. ..

  • Answer # 2

    -Define the structure with the main function.
    -Pass the address of the structure "animals" as an argument to the function "showFastestAnimal".
    -Do not use the subscript operator "[]" in the function "showFastestAnimal".
    -Use the dot operator "." And the indirect operator "*" instead of the arrow operator "->".

    It seems to be difficult to do it with a sword character under some conditions.
    Looking at the conditions in order from the top, first

    -Define the structure with the main function.

    So, I think it will look like this.

    int main ()
    {
      struct XXX {};// Define the structure in main
    }

    The following conditions

    -Pass the address of the structure "animals" as an argument to the function "showFastestAnimal".

    Since the name of the structure was specified in

    int main ()
    {
      struct animals {};// The name of the structure is animals
    }

    When.
    So we have to pass the address to the function.
    I'm sure it's a story of passing the address of an instance of animal type, so

    void showFastestAnimal (void * p) {} // Type is ...
    int main ()
    {
      struct animals {} Obj;// The name of the structure is animals
      showFastestAnimal (&Obj);// pass the address
    }

    Feeling like that?
    There is no specification as to what kind of data the animals type has,

    If you imagine from the name, it seems to hold data on multiple animals,

    Also, from the required results, it seems that at least the name and speed data for each animal are required.

    So, for example, something like this ...?

    struct animal {char name [10];int speed;};// The length of name [] is (provisional)
    struct animals {struct animal data [5];};// The number of animal data is (provisional)

    And in such a state

    -Do not use the subscript operator "[]" in the function "showFastestAnimal".
    -Use the dot operator "." And the indirect operator "*" instead of the arrow operator "->".

    In a form that meets the limitation

    <>
    Fastest Resident: Natalie
    Speed: 70

    Write an implementation in showFastestAnimal () that will be ...
    Well, it would be impossible if there was no such thing as "There are some prerequisites for animals type, and showFastestAnimal () can use them" ...?


    If there may be some assumptions about the arrangement of data ahead of the address passed to showFastestAnimal () and the type of each data, it looks like this ...?

    void showFastestAnimal (
        void * pData, int nAnimal, // There are nAnimal animal data at the location pointed to by pData.
        size_t step_per_animal, // The beginning of the nth animal data is the place offset by n * step_per_animal.
        size_t offset_of_spd, size_t offset_of_name // Position of unsigned int and name (const char *) in one animal data
    )
    {// Restrictions: Do not use [] and->in this
        int i, iMax = 0;
        unsigned int MaxSpeed ​​= 0;
        for (i = 0;i<nAnimal;++ i)
        {
            unsigned int spd = * (unsigned int *) ((unsigned char *) pData + i * step_per_animal + offset_of_spd);
            if (MaxSpeed ​​<spd) {MaxSpeed ​​= spd;iMax = i;}
        }
        printf ("<<Result >>\ n");
        printf ("Fastest Resident:% s \ n", * (const char **) ((unsigned char *) pData + iMax * step_per_animal + offset_of_name));
        printf ("speed:% d \ n", MaxSpeed);
    }
    int main (void)
    {
        typedef struct animal {const char * name;unsigned int speed;} animal;
        struct animals {animal data [5];} Obj;
        Obj.data [0] .name = "nameA";Obj.data [0] .speed = 5;
        Obj.data [1] .name = "nameB";Obj.data [1] .speed = 9;
        Obj.data [2] .name = "nameC";Obj.data [2] .speed = 6;
        Obj.data [3] .name = "Natalie";Obj.data [3] .speed = 70;
        Obj.data [4] .name = "nameE";Obj.data [4] .speed = 3;
        showFastestAnimal (&Obj, 5, sizeof (animal), offsetof (animal, speed), offsetof (animal, name));
        return 0;
    }

  • Answer # 3

    Is it just a typo?
    Just define maxname and maxspeed
    It looks like it hasn't been updated in the for loop (it's updating another variable).