How to put function inside structure? [C]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akif13
    New Member
    • Jan 2013
    • 2

    How to put function inside structure? [C]

    Hello guys. My question is on the title. This is just an exercise that I wanted to do before my final exam. It's quite hard for me to understand. I hope you can give me good explanation or any examples that are related to this question. Sorry for any grammatical errors. English is my 2nd language.

    Question:
    EXERCISE 1
     Create a structure called Car. The members are brand and price.
    In main( ) :
     Create a structure array called C of size 5
     Call function get_input(...), passing in array C
     Call function display(....),p assing in array C
    In function get_input(...) :
     Get user input for brand and price.
    In function display(....) :
     Displaying all car records whose price is more than 80K

    Sample output screen
    Enter car brand : Honda
    Enter car price : RM 79000.00
    Enter car brand : Toyota
    Enter car price : RM 108000.00
    Enter car brand : BMW
    Enter car price : RM 390800.00
    Enter car brand : Kelisa
    Enter car price : RM 32000.00
    Enter car brand : Myvi
    Enter car price : RM 45900.00
    Car brand : Toyota
    Car price : RM 108000.00
    Car brand : BMW
    Car price : RM 390800.00

    My coding:
    Code:
    #include <stdio.h>
    
    struct CAR
    {
        char brand[20];
        float price;
        float get_input(char brand, float price, int i);
        float display(char brand, float price, int i);
    };
    
    int main()
    {
        struct CAR C[5]
        {
            float get_input(char brand, float price, int i);
            float display(char brand, float price, int i);
        }
    
        float get_input(char brand, float price, int i)
        {
            for(i = 0; i < 5; i++))
            {
            printf("Enter car brand : ");
            scanf("%s", C[i].brand);
            printf("\nEnter car price : RM ");
            scanf("%f", &C[i].price);
            }
        }
        float display(char brand, float price, int i)
        {
            if (C[i].price > 80000)
            printf("\nCar brand : %s", C[i].brand);
            printf("\nCar price : RM %.2f", C[i].price);
            else
            printf("\n");
        }
        }
    Errors:
    Line 7: field 'get_input' declared as a function
    line 8: filed 'display' declared as a function
    line 9: warning: useless storage class specifier in empty declaration In function 'main':
    line 14: expected '=', ',', ';', 'asm' or '_attribute_' before '{' token In function 'get_input':
    line 21: expected statement before ')' token
    line 24: 'C' undeclared (first use in this function)
    line 24: (Each undeclared identifier is reported only once In function 'display'
    line 31: 'C' undeclared (first use in this function)
    line 34: 'else' without a previous 'if'
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    In C you cannot put a function inside a struct. You can only put types or pointers to types inside the struct.

    This function prototype:

    Code:
    float get_input(char brand, float price, int i);
    needs to be in the struct as a function pointer. In this case it would be a pointer to a function that takes char, float and int arguments and returns a float. Like this:

    Code:
    float (*get_input)(char brand, float price, int i);
    You would then assign the address of whatever function you want as long as that function has the same three arguments an returns a float.

    Comment

    • akif13
      New Member
      • Jan 2013
      • 2

      #3
      I already fixed it. But when I compile it, it shows nothing. Quite confused here.

      Code:
      #include <stdio.h>
      
      struct CAR
      {
          char brand[20];
          float price;
          float (*get_input)(char brand, float price, int i);
          int (*display)(char brand, float price, int i);
      };
      
      int main()
      {
          struct CAR C[5];
      
          float get_input(char brand, float price, int i)
          {
              for (i = 0; i < 5; i++)
              {
              printf("Enter car brand : ");
              scanf("%s", C[i].brand);
              printf("\nEnter car price : RM ");
              scanf("%f", &C[i].price);
              }
          }
          int display(char brand, float price, int i)
          {
              if (C[i].price > 80000)
              {
              printf("\nCar brand : %s", C[i].brand);
              printf("\nCar price : RM %.2f", C[i].price);
              }
              else
              printf("\n");
          }
          }

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Firstly, you have functions defined inside main(). Functions cannot be defined inside other functions.
        Second, you need to create a Car varable and then assign the address of your function to the function pointer in the variable. Then you can call the function using the variable:

        struct Data
        {
        void (*display)();
        };

        void MyDisplay()
        {
        printf("Hello world!\n");
        }
        int main()
        {
        struct Data var;
        var.display = MyDisplay;
        var.display(); /* function call. you see Hello world! */

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          The problem statement does not direct you to include any functions or function pointers in the structure. It only says that you must provide a structure with two members (brand and price); and provide two functions (get_input() and display()). It goes on to require that those functions must take an entire structure array as one of their parameters. Your get_input() and display() functions do not meet that requirement.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Good point. I missed that. Too much C++, I guess.

            Comment

            Working...