| | |
func return 2 values
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2009
Posts: 36
Reputation:
Solved Threads: 0
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
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();
} Well it works, good job!
Some tips:
Use this form when declaring a struct.
So you don't have to prefix mytype with struct all the time. Now you can do this:
Some tips:
Use this form when declaring a struct.
C Syntax (Toggle Plain Text)
typedef struct { int ok; char ch; } mytype;
So you don't have to prefix mytype with struct all the time. Now you can do this:
C Syntax (Toggle Plain Text)
mytype fundCalculation(int a,int b,int c) { /* ....... */ }
Last edited by minas1; Jan 10th, 2009 at 7:40 am.
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
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.
•
•
Join Date: Jan 2009
Posts: 36
Reputation:
Solved Threads: 0
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;
} C Syntax (Toggle Plain Text)
#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(); }
Instead of passing ints, pass pointers to ints. you did it above; try it and show us, even if it doesn't compiles.
•
•
Join Date: Jan 2009
Posts: 36
Reputation:
Solved Threads: 0
like this ??
C Syntax (Toggle Plain Text)
#include <stdio.h> struct mytype { int ok; char ch; } mytype; 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(); }
Last edited by titosd; Jan 10th, 2009 at 8:32 am.
![]() |
Similar Threads
- How do I recognise strings and numbers (Python)
- How to create a .lib from a .def? (C++)
- Any help is appreciated (C++)
- so I find myself here again (C++)
- Return more than one value from a single function (C++)
Other Threads in the C Forum
- Previous Thread: Thread Scheduling - Please Help
- Next Thread: An implementation idea requied in Compiler Interface
| Thread Tools | Search this Thread |
* ansi append array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram homework i/o ide inches include infiniteloop initialization input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix microsoft multi mysql oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard strchr string suggestions test testautomation testing threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






?!!