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
  #11
Jan 10th, 2009
Ok, let's go step by step.
We will create a struct that has two members, a char and an int.
Go on and declare the struct.
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
  #12
Jan 10th, 2009
grumpier
I didn't ask you to write the func that i want, but I want another example but with parameters to understand the way.
minas I have write the func with structer


#include<stdio.h>
struct mytype
{
int ok;
char ch;
};
 
struct mytype fundCalculation(int a,int b,int c)
{
struct mytype tmp;
if(c==a+b)  {tmp.ok=1;tmp.ch='+';}
else if(c==a-b) {tmp.ok=1;tmp.ch='-';}
else if(c==a*b) {tmp.ok=1;tmp.ch='*';}
else if(c==a/b) {tmp.ok=1;tmp.ch='/';}
else {tmp.ok=0;tmp.ch=' ';}
 
return tmp;
 
}
main()
{
 
 
printf("%i %c ",fundCalculation(3,4,12).ok,fundCalculation(3,4,12).ch);
 
getchar();      
}
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
  #13
Jan 10th, 2009
Well it works, good job!

Some tips:
Use this form when declaring a struct.
  1. typedef struct
  2. {
  3. int ok;
  4. char ch;
  5. } mytype;

So you don't have to prefix mytype with struct all the time. Now you can do this:

  1. mytype fundCalculation(int a,int b,int c)
  2. {
  3. /* ....... */
  4. }
Last edited by minas1; Jan 10th, 2009 at 7:40 am.
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
  #14
Jan 10th, 2009
yes I know that it works, but in the question we should write the func just with pointers !!!
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
  #15
Jan 10th, 2009
Then instead of ints in the argument list, use pointers to ints.

a pointer to int: int *a = 0;

when you call the function, you need to pass addresses.

so

int x = 4;
callSomeFuncTakingPointers(&x);

when you need to access a value stored in a pointer, you the * operator.
int x = 3;
int *p = &x;
printf(%d, *p);

If you want to learn more about pointers,
http://www.cplusplus.com/doc/tutorial/pointers.html
Last edited by minas1; Jan 10th, 2009 at 8:01 am.
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
  #16
Jan 10th, 2009
like this ?

int findCalculation(int *a, int *b, int *c)
{
	char ch;
	if (*a - *b == *c)
		return 1;
	else return 0;

	if (*a + *b == *c)
		return 1;
	else return 0;

	if (*a * *b == *c)
		return 1;
	else return 0;

	if (*a / *b == *c)
		return 1;
	else return 0;

}
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
  #17
Jan 10th, 2009
Yes, but use our function that returns the struct.
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
  #18
Jan 10th, 2009
How can I do this ?!!
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
  #19
Jan 10th, 2009
  1. #include<stdio.h>
  2. struct mytype
  3. {
  4. int ok;
  5. char ch;
  6. };
  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.  
  24. printf("%i %c ",fundCalculation(3,4,12).ok,fundCalculation(3,4,12).ch);
  25.  
  26. getchar();
  27. }

Instead of passing ints, pass pointers to ints. you did it above; try it and show us, even if it doesn't compiles.
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
  #20
Jan 10th, 2009
like this ??

  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.  
  24. printf("%i %c ",fundCalculation(3,4,12).ok,fundCalculation(3,4,12).ch);
  25.  
  26. getchar();
  27. }
Last edited by titosd; Jan 10th, 2009 at 8:32 am.
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