Solved

returned values in ANSI C

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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.
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now