Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Returning two values (http://www.programmingforums.org/showthread.php?t=4415)

Navid Jun 12th, 2005 11:51 AM

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() );

L7Sqr Jun 12th, 2005 12:35 PM

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...

kirkl_uk Jun 12th, 2005 12:45 PM

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.

L7Sqr Jun 12th, 2005 12:55 PM

Agreed, references and bool return values would suffice in C++, but for a C program, they will not be of much assistance...

Dameon Jun 12th, 2005 1:58 PM

C and C++ both have the concept of references and pointers last I checked

kirkl_uk Jun 12th, 2005 2:04 PM

Maybe he is referring to the bool type.

uman Jun 12th, 2005 2:19 PM

C has references?

Ancient Dragon Jun 12th, 2005 2:21 PM

Quote:

Originally Posted by uman
C has references?

yes
:

// c++ reference
void foo(int& x)
{
  x = 0;
}

// C reference
void foo(int* x)
{
  *x = 0;
}


stevengs Jun 12th, 2005 2:28 PM

Quote:

Originally Posted by Ancient Dragon
yes
:



well, I reckon I am more confused than I had originally believed myself to be. I was certain that there were differences between references and pointers. My main argument would probably be: "Then why be there??" Isn't the use of references more restrictive (i.e. for safety) ?

I was also under the impression that standard C does not specify the use of references.. just goes to show what I know...

DaWei Jun 12th, 2005 2:35 PM

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