func return 2 values

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: func return 2 values

 
0
  #21
Jan 10th, 2009
The function is correct, but you don't call it correctly.
Create 3 variables in main x, y, z, give them values and pass their address to the function.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 36
Reputation: titosd is an unknown quantity at this point 
Solved Threads: 0
titosd titosd is offline Offline
Light Poster

Re: func return 2 values

 
0
  #22
Jan 10th, 2009
like this ??

if yes, but we use the structer, and we should just use in the function the pointers without anything else ....

this is the code:

  1. #include <stdio.h>
  2. struct mytype
  3. {
  4. int ok;
  5. char ch;
  6. } mytype;
  7.  
  8. struct mytype fundCalculation(int *a,int *b,int *c)
  9. {
  10. struct mytype tmp;
  11. if(*c==*a+*b) {tmp.ok=1;tmp.ch='+';}
  12. else if(*c==*a-*b) {tmp.ok=1;tmp.ch='-';}
  13. else if(*c==*a * *b) {tmp.ok=1;tmp.ch='*';}
  14. else if(*c==*a / *b) {tmp.ok=1;tmp.ch='/';}
  15. else {tmp.ok=0;tmp.ch=' ';}
  16.  
  17. return tmp;
  18.  
  19. }
  20. main()
  21. {
  22.  
  23. int x=3;
  24. int y=4;
  25. int z=12;
  26.  
  27. printf("%i %c ",fundCalculation(&x,&y,&z).ok,fundCalculation(&x,&y,&z).ch);
  28.  
  29. getchar();
  30. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: func return 2 values

 
0
  #23
Jan 10th, 2009
Yes.

In main, it's a better idea to actually create an oject
int main()
{
struct mytype mt = fundCalculation(....)
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 36
Reputation: titosd is an unknown quantity at this point 
Solved Threads: 0
titosd titosd is offline Offline
Light Poster

Re: func return 2 values

 
0
  #24
Jan 10th, 2009
thank you very much minas1

thanks again for helping and tips
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: func return 2 values

 
0
  #25
Jan 10th, 2009
Nothing. Now show us the complete program. also use the typedef for the struct like I told you.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 36
Reputation: titosd is an unknown quantity at this point 
Solved Threads: 0
titosd titosd is offline Offline
Light Poster

Re: func return 2 values

 
0
  #26
Jan 10th, 2009
Ok, I will.
but the program is very long. contain some function, when I finish it I will put it here
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: func return 2 values

 
0
  #27
Jan 13th, 2009
I think what you want to do is just return the one or zero rather than a struct. The operation can be passed as a pointer to a char (in the parameters). a, b, and c don't need to be pointers.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC