gurulet aptitude test (and bug)

Wayne A. Throop throopw at xyzzy.UUCP
Sat Dec 17 03:07:43 AEST 1988


> jpa at celerity.UUCP (Jeff Anderson)
> I submit it only as an interesting puzzle
> [... example appended to end of article for reference ...]

It's more than that.  It shows a place where lint gives a misleading-at-best
diagnostic.  In particular, lint says

    sub2: variable # of args.       t1.c(19)  ::  t1.c(16)
    sub1: variable # of args.       t1.c(13)  ::  t1.c(27)
    sub1 value is used, but none returned
    sub2 returns value which is always ignored

The oddity is "sub1 value is used, but none is returned".  The value
isn't, in fact, used.  But apparently many compilers generate code
which fails if a function declared to return a struct does not do so.

This problem would be much less probable if one always declared one's
function types explicitly.  And paid attention to what lint tells one,
of course.

/***************************************************\
* 						    *
* What is wrong with this program? This is a test.  *
* 						    *
\***************************************************/
struct zztop {
    int             a;
    int             b;
}
/* main calls sub1 which calls sub2 */
#define UGLY 0x77777777
sub1()
{
	struct zztop zz;
	zz.a = UGLY;
	sub2(zz);
}
sub2()
{
	struct zztop zz;
	zz.b = UGLY;
	return(UGLY);
}
main()
{
	struct zztop zz;
	sub1(&zz);
}
--
The real problem is not whether machines think, but whether men do.
                                        --- B.F. Skinner
-- 
Wayne Throop      <the-known-world>!mcnc!rti!xyzzy!throopw



More information about the Comp.lang.c mailing list