943,754 Members | Top Members by Rank

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

Re: func return 2 values

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.
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

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();      
}
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

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.
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

yes I know that it works, but in the question we should write the func just with pointers !!!
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

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.
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 ?

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;

}
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, but use our function that returns the struct.
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

How can I do this ?!!
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

  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.
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 ??

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

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