![]() |
Returning two values (solved)
Is is possible to have a function to return two values instead of one?
Or do i have to have two functions for each value? :
printf(" The two values returned: %d and %d", funct() ); |
You will want to use a structure to do what you are asking.
example: :
typedef struct {From there you can either pass in a pointer to a function and fill in the values or use a structure (or pointer to) as a return value for the function. And, btw, when you are calling a printf function (or any of the varidic funtions) You want to provide enough arguments for each of the format specifiers. i.e. for two %d's you will want two arguments passed to the function. I assume you did that just for an example of your question, but thought I would comment anyway... |
Just for two integers, another option would be to provide references to the two:
:
bool funct(int& x, int& y) {Then you call the function like so: :
int x, y;This allows for you to return whether the function succeeded as well. Both methods put forward in this thread will work. |
Agreed, references and bool return values would suffice in C++, but for a C program, they will not be of much assistance...
|
C and C++ both have the concept of references and pointers last I checked
|
Maybe he is referring to the bool type.
|
C has references?
|
Quote:
:
// c++ reference |
Quote:
I was also under the impression that standard C does not specify the use of references.. just goes to show what I know... |
There is definitely a difference between a "reference" per C++ definition and a pointer. The choice of the term, "reference" for C++, was a little shaky, as pointers, labels, nicknames, aliases, are ALL references. However, one cannot pass a "reference" (as defined in C++) in C. The "&" operator is strictly an address-of operator and the use inside the function has to be the same as when dereferencing a pointer. In C++, on the other hand, a "reference" is an alias and a passed reference amounts to one fewer levels of indirection.
|
| All times are GMT -5. The time now is 4:32 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC