double *foo(double *x, int *ret)
{
double *x;
int sz;
x = malloc(100);
/* do something on x */
/* compute sz */
ret = malloc(sz);
/* do something on y*/
return x;
}
In foo, sz's value is computed during running. My question is how to
return
x and ret in the same time, without using a structure like the
following?
struct ret_val_t {
double *x;
int *ret;
}
One solution might be to use a global variable, int *ret. But I think
this is not a good solution since people usually recommend agaist
using global variables. 9 1686
On Fri, 11 Apr 2008 21:23:52 -0700 (PDT), is*********@gma il.com wrote:
>double *foo(double *x, int *ret) {
double *x;
You don't want two definitions of x.
int sz;
x = malloc(100);
/* do something on x */
/* compute sz */
ret = malloc(sz);
/* do something on y*/
return x; }
In foo, sz's value is computed during running. My question is how to return x and ret in the same time, without using a structure like the following?
One common method is to change the function definition to
double *foo(double *x, int **ret)
and within the function change ret to *ret.
In the calling function, instead of calling the function with
foo(my_double_p ointer, my_int_pointer) ;
you would use
foo(my_double_p ointer, &my_int_pointer )
If you chose the method, I would do it for both arguments and change
the function to return nothing (void) or to return an int success
flag.
> struct ret_val_t {
double *x;
int *ret; }
One solution might be to use a global variable, int *ret. But I think this is not a good solution since people usually recommend agaist using global variables.
When you start to write complicated programs with many functions, you
will appreciate this advice.
Remove del for email
On Apr 12, 1:00 am, Barry Schwarz <schwa...@dqel. comwrote:
When you start to write complicated programs with many functions, you
will appreciate this advice.
Thanks.
Remove del for email
I don't understand this. is*********@gma il.com wrote:
On Apr 12, 1:00 am, Barry Schwarz <schwa...@dqel. comwrote:
[ ... ]
>Remove del for email
I don't understand this.
What's not to understand? It means that you need to remove the three
characters: d, e, and l from his email address to get a valid address
to contact him.
On Apr 12, 9:23 am, istillsh...@gma il.com wrote:
double *foo(double *x, int *ret)
{
double *x;
int sz;
x = malloc(100);
/* do something on x */
/* compute sz */
ret = malloc(sz);
/* do something on y*/
return x;
}
In foo, sz's value is computed during running. My question is how to
return
x and ret in the same time, without using a structure like the
following?
struct ret_val_t {
double *x;
int *ret;
}
Try using double pointers. In the calling function don't pass the copy
of the pointer, instead pass the address of the pointer. Your pointer
will get modified.
void foo(double **x, int **ret)
{
}
void calling_functio n()
{
double *x;
int *ret;
foo(&x, &ret);
}
Also read FAQ 4.8
pereges said:
<snip>
Try using double pointers.
Because C has a data type 'double', the phrase 'double pointer' is
ambiguous. It is clearer to say 'pointer to pointer'. (But yes, I knew
what you meant, and yes, that's one way to solve his problem.)
<snip>
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999 is*********@gma il.com wrote:
double *foo(double *x, int *ret)
{
double *x;
int sz;
x = malloc(100);
/* do something on x */
/* compute sz */
ret = malloc(sz);
/* do something on y*/
return x;
}
In foo, sz's value is computed during running. My question is how to
return
x and ret in the same time, without using a structure like the
following?
The advice you've received has been to return the values in the parameters,
so (to use a much simpler example):
#include<stdio. h>
void returnpair(int *a,int *b) {
*a+=100;
*b+=200;
}
int main(void) {
int a,b;
a=20;
b=30;
returnpair(&a,& b);
printf("A', B' = %d %d\n",a,b);
}
You need an extra level of pointers (hence the ** for your example). However
this doesn't address your question (and will also overwrite your
parameters).
struct ret_val_t {
double *x;
int *ret;
}
This is the natural solution; what's wrong with this?
One solution might be to use a global variable, int *ret. But I think
this is not a good solution since people usually recommend agaist
using global variables.
If it works, use it until you have time to do it better.
Another way is to return the parameters one at a time, but it could mean
calculating twice, depending on how you arrange your function, and you need
an extra parameter to select the return value:
#include<stdio. h>
int returnpair(int a,int b, int n) {
a+=100;
b+=200;
if (n==1) return a;
return b;
}
int main(void) {
int a,b,anew,bnew;
a=20;
b=30;
anew = returnpair(a,b, 1); /* must not change a,b between calls */
bnew = returnpair(a,b, 2);
printf("A', B' = %d %d\n",anew,bnew );
}
--
Bart
On Apr 11, 11:23 pm, istillsh...@gma il.com wrote:
double *foo(double *x, int *ret)
{
double *x;
int sz;
x = malloc(100);
/* do something on x */
/* compute sz */
ret = malloc(sz);
/* do something on y*/
return x;
}
In foo, sz's value is computed during running. My question is how to
return
x and ret in the same time, without using a structure like the
following?
struct ret_val_t {
double *x;
int *ret;
}
One solution might be to use a global variable, int *ret. But I think
this is not a good solution since people usually recommend agaist
using global variables.
Yeah, you generally do not want to rely on global variables (they
promote tight coupling between modules and make code harder to
reuse).
If the values you are returning naturally make up a composite type
(such as fields in a street address), use a struct to group them
together and return an instance of the struct. If the values you are
returning do not naturally make up a composite type (such as a buffer,
buffer length, and read status), use separate writable function
parameters.
In article <d3************ *************** *******@a1g2000 hsb.googlegroup s.com>,
John Bode <jo*******@my-deja.comwrote:
....
>Yeah, you generally do not want to rely on global variables (they promote tight coupling between modules and make code harder to reuse).
Luckily, since C doesn't have global variables (ask anyone in this
group!), you have nothing to fear here. ga*****@xmissio n.xmission.com (Kenny McCormack) writes:
In article <d3************ *************** *******@a1g2000 hsb.googlegroup s.com>,
John Bode <jo*******@my-deja.comwrote:
...
>>Yeah, you generally do not want to rely on global variables (they promote tight coupling between modules and make code harder to reuse).
Luckily, since C doesn't have global variables (ask anyone in this
group!), you have nothing to fear here.
Serious question for you - having read all Heathfield's bluster, is
there any way you could be convinced that C does not have "global
variables"? I know that I shake my head more and more when I read his
bullshit. What I want to know is , what the hell does he and the other
clique members think they are gaining by playing such silly games? I
have worked on more big C projects than I care to remember and not once
did any one get confused when "globals" where mentioned in the context
of the program. Why here in c.l.c? Really. My mind boggles at their self
important posturing in the face of common sense. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Darren Dale |
last post by:
Hello,
def test(data):
i = ? This is the line I have trouble with
if i==1: return data
else: return data
a,b,c,d = test()
|
by: Varun |
last post by:
Hi There,
I have a form("myRequest.asp") and the values from it are
retrieved into the page ("output_Print.asp") on which I have two
buttons('Save As Complete' and 'Save As Incomplete'). When the 'Save
as Incomplete' button is Clicked the form will be going to the
"SaveAsincomplete.asp" without validation of the fields. And when the
'save as complete' is clicked certain fileds are to be validated and
by the function return value, if false...
|
by: Petr Bravenec |
last post by:
I have found that when I use the RETURN NEXT command in recursive function,
not all records are returned. The only records I can obtain from function
are records from the highest level of recursion. Does exist some
work-around?
Thanks
Petr Bravenec
example:
create table foo (
|
by: Ravindranath Gummadidala |
last post by:
Hi All:
I am trying to understand the C function call mechanism. Please bear with
me as I state what I know:
"every invocation of a function causes a frame for that function to be
pushed on stack. this contains the arguments this function was called
with, address to return to after return from this function (the location
in the previous stack frame), location of previous frame on stack (base or
start of this frame) and local variables...
|
by: D. Shane Fowlkes |
last post by:
This may be a very basic question but it's something I've never done before.
I've looked at a couple of my favorite sites and books and can't find an
answer either. I can write a Function to return a single value. No big
deal. But I want to call a Function from another Sub and the function finds
and returns an entire db record. Using ASP.NET (VB!), how can this be done
and how can I differentiate between the fields/columns?
For...
| |
by: Nikolay Petrov |
last post by:
How can I return multiple values from a custom function?
TIA
|
by: aleksandar.ristovski |
last post by:
Hello all,
I have been thinking about a possible extension to C/C++ syntax.
The current syntax allows declaring a function that returns a value:
int foo();
however, if I were to return more than one value, for example three
int-s, I would have to change my "logic" and pass the references to my
|
by: xicloid |
last post by:
I'm making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I don't know how to do that.
Isn't there anything like "return null"in C?
|
by: barcaroller |
last post by:
I am trying to adopt a model for calling functions and checking their return
values. I'm following Scott Meyer's recommendation of not over-using
exceptions because of their potential overhead.
Here's the approach I'm currently looking at. I throw exceptions only from
constructors. Destructors, of course, do not throw exceptions. All other
functions return a signed integer. The values are all stored in one large
header file (as...
|
by: ADezii |
last post by:
The incentive for this Tip was an Article by the amazing Allen Browne - I considered it noteworthy enough to post as The Tip of the Week in this Access Forum.
Original Article by Allen Browne
Traditionally, one has always thought that Functions can only return a single value, and for the most part that was true. Ever since Access 95, we gained the new functionality, through VBA, to have Functions return an entire Structure of values. A User...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |