This topic has been archived. It cannot be replied.
-
工作学习 / IT技术讨论 / 问题讨论,考考你的C语言(a lillte bit more than that)知识很有趣哟
-eagle_no1(瞎起哄);
2003-6-14
{573}
(#1239588@0)
-
The code can pass compilation. The result is e. unexpectedThe root-cause of the bug is the priority of "[]" is greater than "++". That results in sum = aa[20]+aa[19]+...
To avoid the operator priority error, I would suggest modify the code in simple way:
{
i --;
sum += aa[i];
}
aa[MAX_LEN] is acceptable in most popular compliers, but it may not be passed in old C compiler like C80.
-jami(jami);
2003-6-14
{359}
(#1239620@0)
-
great! because you mentioned the compiler,Butwhat is the value of the aa[20]? is it consistant or just random?
-eagle_no1(瞎起哄);
2003-6-14
{67}
(#1239629@0)
-
aa[20] would be located in the position of the space for int i=20 in the used stack.I'm sure its value should be 20 prior to do i--. Therefore, the sum is supposed to be 20.
-jami(jami);
2003-6-14
{90}
(#1240310@0)
-
glad to discuss with you, actually I am confussed now, some compiler's behavior is too strange!!
-eagle_no1(瞎起哄);
2003-6-14
{6664}
(#1240400@0)
-
exactly. your outcome is correct. I've confirmed it in Borland C++.As to BC compiler, the stack map is as follow:
low addr: i = 20
aa[0]...[20]
pad = 2
sum = 0
return address
high addr ...
aa[20]'s location in memory points to the space of pad. That's why the sum should be 2 all the time.
However, the result depends on the C compiler for the location of aa[20].
-jami(jami);
2003-6-15
{461}
(#1240663@0)
-
oh, really? shakehand!
-eagle_no1(瞎起哄);
2003-6-15
(#1240773@0)
-
大家平时是靠调试找出这些错误呢?还是光看源码就行了,这就太牛了!
-m30(m30);
2003-6-14
(#1240346@0)
-
experience pus debuging
-eagle_no1(瞎起哄);
2003-6-14
(#1240411@0)
-
有什么意义啊! 30岁以后还有为代码技巧和识别编译错误自豪的吗?
-needacar123(car);
2003-6-14
(#1240417@0)
-
just technical discussion , no thing more, OK?
-eagle_no1(瞎起哄);
2003-6-14
(#1240434@0)
-
OK,OK,OK.我跟M30说话呢,我不掺和你的technical discussion .
-needacar123(car);
2003-6-14
(#1240456@0)
-
误会,误会,原来你们认识, 接着骂,接着骂。。我的意思是”只是技术讨论而已(当然是低级技术),没有自豪的意思,是吗!“, 言不达意,见谅,见谅!
-eagle_no1(瞎起哄);
2003-6-15
{95}
(#1240501@0)
-
我根本不认识他啊
-m30(m30);
2003-6-15
(#1240775@0)
-
To m30:from you old posts, I found you graduated form an U. in wuhan, probably we know each other. I came from wuhan (HUST when 6.4)
-eagle_no1(瞎起哄);
2003-6-15
{126}
(#1240783@0)
-
啊,有人在暗中收集我的信息啊?可怕!HUST too。不过到此为止,别再追问细节了啊?见同学情更怯啊!
-m30(m30);
2003-6-15
(#1240787@0)
-
sorry! :-(
-eagle_no1(瞎起哄);
2003-6-15
(#1240789@0)
-
看来以后我要做个好人了,这儿还有同学在盯着呢!
-m30(m30);
2003-6-15
(#1240793@0)
-
我算是知道了什么是“自作多情”兄弟,没事,接着当坏人。
噢,别忘了,下次丢人显眼的时候,别说是HUST的 。。。就说你是。。。。武大的 :P
-eagle_no1(瞎起哄);
2003-6-15
{103}
(#1240822@0)
-
.呸,你一肚子坏水。偶看见就宣传他是HUST的。:))
-henhen(哼哼,找工ing...);
2003-6-15
{48}
(#1240837@0)
-
:-(别台举他呀,大姐,?啊! 我知道你的难处了,下回就说你没读过书不就得了。 :-(
-eagle_no1(瞎起哄);
2003-6-15
{76}
(#1240850@0)
-
:-(说偶没读过书倒是真的。
-henhen(哼哼,找工ing...);
2003-6-15
{22}
(#1240853@0)
-
don't be sad, lady.just kidding LA
-eagle_no1(瞎起哄);
2003-6-15
(#1240862@0)
-
我知道你是UofW的,我们都不要互相结发了,好吧?
-m30(m30);
2003-6-15
(#1240865@0)
-
正中我的痛处
-tztz(退休老团长);
2003-6-15
(#1240794@0)
-
If you folks are genuinely up to improve your C skills. Go get a book by Sun Microsystems' Peter Van Der Linden, you'll be amazed how deep the C world is.
-hairypotter(HP);
2003-6-15
(#1240826@0)
-
Peter's web site.
-hairypotter(HP);
2003-6-15
(#1240838@0)
-
Sample question from the book: why Halloween = Christmas?
-hairypotter(HP);
2003-6-15
(#1240842@0)
-
My trouble is "why Halloween !=Christmas" see #1240400
-eagle_no1(瞎起哄);
2003-6-15
(#1240856@0)