| | |
func return 2 values
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2009
Posts: 39
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: 39
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: 39
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
Views: 1281 | Replies: 26
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall testautomation unix user variable voidmain() wab win32 win32api windows.h






?!!