Solved

returned values in ANSI C

Posted on 2004-04-18
4
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Eclipse Java import and method not resolved 4 124
IDE for Python 5 104
How do I fix this situation in GIT? 2 72
How do I buy BitCoin? 4 25
A short article about a problem I had getting the GPS LocationListener working.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

739 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