![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
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() );Last edited by navnav; Jun 12th, 2005 at 4:47 PM. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 114
Rep Power: 0
![]() |
You will want to use a structure to do what you are asking.
example: typedef struct {
int x;
int y;
} two_int_t;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... |
|
|
|
|
|
#3 |
|
Programmer
|
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;
if(funct(x, y)) {
printf("The two values returned: %d and %d", x, y);
}This allows for you to return whether the function succeeded as well. Both methods put forward in this thread will work.
__________________
kirkl_uk |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 114
Rep Power: 0
![]() |
Agreed, references and bool return values would suffice in C++, but for a C program, they will not be of much assistance...
|
|
|
|
|
|
#5 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
C and C++ both have the concept of references and pointers last I checked
|
|
|
|
|
|
#6 |
|
Programmer
|
Maybe he is referring to the bool type.
__________________
kirkl_uk |
|
|
|
|
|
#7 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
C has references?
|
|
|
|
|
|
#8 | |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 498
Rep Power: 4
![]() |
Quote:
// c++ reference
void foo(int& x)
{
x = 0;
}
// C reference
void foo(int* x)
{
*x = 0;
} |
|
|
|
|
|
|
#9 | |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Quote:
I was also under the impression that standard C does not specify the use of references.. just goes to show what I know...
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty Last edited by stevengs; Jun 12th, 2005 at 11:36 PM. |
|
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|