943,777 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2098
  • C RSS
You are currently viewing page 3 of this multi-page discussion thread; Jump to the first page
Jan 10th, 2009
0

Re: func return 2 values

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.
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Jan 10th, 2009
0

Re: func return 2 values

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. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
titosd is offline Offline
56 posts
since Jan 2009
Jan 10th, 2009
0

Re: func return 2 values

Yes.

In main, it's a better idea to actually create an oject
int main()
{
struct mytype mt = fundCalculation(....)
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Jan 10th, 2009
0

Re: func return 2 values

thank you very much minas1

thanks again for helping and tips
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
titosd is offline Offline
56 posts
since Jan 2009
Jan 10th, 2009
0

Re: func return 2 values

Nothing. Now show us the complete program. also use the typedef for the struct like I told you.
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Jan 10th, 2009
0

Re: func return 2 values

Ok, I will.
but the program is very long. contain some function, when I finish it I will put it here
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
titosd is offline Offline
56 posts
since Jan 2009
Jan 13th, 2009
0

Re: func return 2 values

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.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Thread Scheduling - Please Help
Next Thread in C Forum Timeline: An implementation idea requied in Compiler Interface





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC