Solved

returned values in ANSI C

Posted on 2004-04-18
4
228 Views
Last Modified: 2011-09-20
Well I'm not the best when it comes to ANSI C programming...but i thought this was for sure.
Let's say we have the code

#include <stdio.h>

int test()
{
     return 1;

}

void main()
{
     int t;
     t=test();
     printf("t=%d\n", &t);
}

I expected that the output at the screen would be "t=1"....It's not it's something like 1245052 and doen't seem somehow connected to the value the test() functin returns...
The problem is how can I save the value that a function returns into a variable...In this specific case how can I save somewhere the value "1".
0
Comment
Question by:jonn_g
  • 2
4 Comments
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 200 total points
ID: 10852738
This has nothing to do with the return value, the problem is that you are passing the address of t to the printf function, and not t. Use this:

printf("t=%d\n", t);

You probably got this confused with scanf, which needs and address so that the value of the variable can actually be modified. You need to understand the difference between "pass by value" and "pass by reference"
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10852742
... and just in case it's not clear, the value "1" was already correctly saved in the "t" variable.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
A short article about problems I had with the new location API and permissions in Marshmallow
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question