Groups | Search | Server Info | Keyboard shortcuts | Login | Register
Groups > comp.lang.c.moderated > #425
| From | 谢成骏 <bbboson@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c.moderated |
| Subject | why I got this answer? |
| Date | 2013-02-26 10:51 -0600 |
| Organization | Usenet Fact Police |
| Message-ID | <clcm-20130226-0010@plethora.net> (permalink) |
Hi all.
I write a small program. But I can't understand the result of it. here is my code:
-----------------------------------------
#include <stdio.h>
int* foo(int n)
{
int *p = &n;
return p;
}
int f(int m)
{
int n = 1;
return 999;
}
int main(int argc, char *argv[])
{
int num = 1;
int *p = foo(num);
int q = f(999);
printf("[%d]\n[%d]\n", *p, q);
}
-----------------------------------------
output:
[999]
[999]
Why *p is 999?
Then I changed my code:
-----------------------------------------
#include <stdio.h>
int* foo(int n)
{
int *p = &n;
return p;
}
int f()
{
int n = 1;
return 999;
}
int main(int argc, char *argv[])
{
int num = 1;
int *p = foo(num);
int q = f();
printf("[%d]\n[%d]\n", *p, q);
}
------------------------------------
output:
[1]
[999]
Why *p is 1? I can't understand why.
--
Thanks,
Madper Xie.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to comp.lang.c.moderated | Previous | Next — Next in thread | Find similar
why I got this answer? 谢成骏 <bbboson@gmail.com> - 2013-02-26 10:51 -0600 Re: why I got this answer? Jasen Betts <jasen@xnet.co.nz> - 2013-03-11 18:25 -0500 Re: why I got this answer? Barry Schwarz <schwarzb@dqel.com> - 2013-03-11 18:25 -0500 Re: why I got this answer? Thomas Richter <thor@math.tu-berlin.de> - 2013-03-11 18:25 -0500
csiph-web