473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Retrieving a value from a void function.

Don
I am a beginner in C. A student I should say. Is it possible to
retrieve a value from a void function? A have a void function that
reads a string or number from a file. I call that function from main
and want to set char* x or int x equal to what is read. How does that
work?
Thank you

Nov 14 '05 #1
3 1433
Hi,

You can't read back a value from a void function. For your problem, you
have two options. You've to chage the function declaration so that it
returns the desired value (int or char *) or you have to pass a pointer
as an argument to the void function. Look at the following snippets:

Option 1:
---------------
main(){
int x;
x = foo();
printf("%d\n", x);
}

int foo(){
int y;
/*read from the file and store the value in y*/
return y;
}

Option 2:
-----------------
main(){
int x;

foo(&x);
printf("%d\n", x);
}

void foo(int *x){

/*read from file and store it in x*/
return;
}

Nov 14 '05 #2
In article <11**********************@z14g2000cwz.googlegroups .com>,
Jyotirmoy <jy*******@gmail.com> wrote:
You can't read back a value from a void function. For your problem, you
have two options. You've to chage the function declaration so that it
returns the desired value (int or char *) or you have to pass a pointer
as an argument to the void function.


Another possibility suitable in -some- instances is to set a variable
that is in the scope of both the called and calling function; that
could be file scope or a "global variable".

And a different hack that is sometimes used in difficult situations is,
on systems that support it (as it is not pure C) to set an environment
variable.

In between, there are possibilities involving temporary files, or
pipes, or shared memory segments, or STREAMS... definitely not all
portable.
--
'ignorandus (Latin): "deserving not to be known"'
-- Journal of Self-Referentialism
Nov 14 '05 #3
Don
The argument to the void function was indeed a pointer. Thank you
kindly. That was a life saver.

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jeffrey B. Holtz | last post by:
I'm trying to get the Name of the USB device pluged in from the RegisterDeviceNotification that I've used P/Invoke to marshal. I have seen a similar posting on the VisualBasic newgroups but I do...
6
by: niju | last post by:
Hi there I wired up the Javascript function "Business" OnPreRender Event protected override void OnPreRender(EventArgs e) { optBussiness.Attributes.Add("OnClick", "Business();") } Here is...
6
by: yasodhai | last post by:
Hi, I used a dropdown control which is binded to a datagrid control. I passed the values to the dropdownlist from the database using a function as follows in the aspx itself. <asp:DropDownList...
2
by: 4Ankit | last post by:
hello all, i am having some difficulty retrieving information from my form. I want to add content in my table but the content i want to add to the table is what the user inputs in my form. ...
1
by: Sue | last post by:
Hello All I have a typed dataset called PropertyDS, with 2 colums named - fld_propertyname and fld_propertyvalue. From my web app, I store values in these columns with a Add_row function. So, it...
4
by: kaisersose1995 | last post by:
Hi, I'm trying to get Novell Netware information using vba into my access database and have no idea how to progress. Basically when i right click the properties on my computer the free space of a...
0
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options...
0
by: Phijo | last post by:
Hello, I'm a Java developer but brand new to ASP (i have done some of the tutorials) so I have some ASP programming background however I cannot find the cause or the fix to this problem. I am...
1
by: fidgen | last post by:
Hiya, I'm trying to get a AJAX driven update to my list of news articles, so when users click the title of the news article, it pops up the article content in a thickbox overlay. Retrieving...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.