Solved

returned values in ANSI C

Posted on 2004-04-18
4
240 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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…
Six Sigma Control Plans

691 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