473,320 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to put function inside structure? [C]

2
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(....),passing 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:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. struct CAR
  4. {
  5.     char brand[20];
  6.     float price;
  7.     float get_input(char brand, float price, int i);
  8.     float display(char brand, float price, int i);
  9. };
  10.  
  11. int main()
  12. {
  13.     struct CAR C[5]
  14.     {
  15.         float get_input(char brand, float price, int i);
  16.         float display(char brand, float price, int i);
  17.     }
  18.  
  19.     float get_input(char brand, float price, int i)
  20.     {
  21.         for(i = 0; i < 5; i++))
  22.         {
  23.         printf("Enter car brand : ");
  24.         scanf("%s", C[i].brand);
  25.         printf("\nEnter car price : RM ");
  26.         scanf("%f", &C[i].price);
  27.         }
  28.     }
  29.     float display(char brand, float price, int i)
  30.     {
  31.         if (C[i].price > 80000)
  32.         printf("\nCar brand : %s", C[i].brand);
  33.         printf("\nCar price : RM %.2f", C[i].price);
  34.         else
  35.         printf("\n");
  36.     }
  37.     }
  38.  
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'
Jan 12 '13 #1
5 2787
weaknessforcats
9,208 Expert Mod 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. 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.
Jan 12 '13 #2
akif13
2
I already fixed it. But when I compile it, it shows nothing. Quite confused here.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. struct CAR
  4. {
  5.     char brand[20];
  6.     float price;
  7.     float (*get_input)(char brand, float price, int i);
  8.     int (*display)(char brand, float price, int i);
  9. };
  10.  
  11. int main()
  12. {
  13.     struct CAR C[5];
  14.  
  15.     float get_input(char brand, float price, int i)
  16.     {
  17.         for (i = 0; i < 5; i++)
  18.         {
  19.         printf("Enter car brand : ");
  20.         scanf("%s", C[i].brand);
  21.         printf("\nEnter car price : RM ");
  22.         scanf("%f", &C[i].price);
  23.         }
  24.     }
  25.     int display(char brand, float price, int i)
  26.     {
  27.         if (C[i].price > 80000)
  28.         {
  29.         printf("\nCar brand : %s", C[i].brand);
  30.         printf("\nCar price : RM %.2f", C[i].price);
  31.         }
  32.         else
  33.         printf("\n");
  34.     }
  35.     }
  36.  
Jan 12 '13 #3
weaknessforcats
9,208 Expert Mod 8TB
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! */
Jan 12 '13 #4
donbock
2,426 Expert 2GB
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.
Jan 12 '13 #5
weaknessforcats
9,208 Expert Mod 8TB
Good point. I missed that. Too much C++, I guess.
Jan 12 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
1
by: Maxim Kazitov | last post by:
Hi, Is it possible to call C# function inside XSLT ? Thanks, Max
3
by: darrel | last post by:
In my XSLT, I'm trying to write out some dynamic URLs. I can't put an actual xsl:value-of field inside a URL: <a href="<xsl:value-of select="linkUrl"/>"> But I can do this: <a...
2
by: Syl | last post by:
Hi Group - I have a function to send an email to a client after they have successfully submitted a form on my site. I have a 2nd function that prints their input to the browser as a confirmation....
0
by: ahcirlko | last post by:
I am using different user controls in same page. I have a function inside of the .ascx file. How can I call it from another .ascx file Language C#
1
denizvb
by: denizvb | last post by:
Hai programmers, Do you know How to write a function inside another function in c
1
by: jadeite100 | last post by:
Hi: I am having problem passing an xsl param variable as a parameter to a javascript function inside an xsl styelsheet. Here is my xsl stylesheet. I am trying to pass a variable called...
0
by: ponvijaya | last post by:
Hi all, I have used a substring function inside my stored procedre as set @dummy1=substring(@dummyid,2); but when i print and check the value of @dummy1 as select @dummy1;
4
by: backups2007 | last post by:
is it possible to use an include function inside an isset function? something like: if(isset($_POST)) { include("insert.php"); }
4
by: MasterVision | last post by:
Hey folks, I have .NET Solution with the combinations of VB, C/C++ and C# projects on it. I have no problem calling C/C++ function inside my VB project but I dont know how to do the same with C#...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.