| | |
func return 2 values
![]() |
•
•
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 |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf scripting segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






?!!