Is there a good example of how toupper() works?

Blair P. Houghton bhoughto at cmdnfs.intel.com
Wed Oct 17 07:59:16 AEST 1990


Sorry if anyone saw my earlier posting to this thread; a) I thought
I was mailing it; b) I thought I had hit 'l' for 'list' instead
of 's' for 'send': and there were several things yet to edit.
I went after ^C, but it was too late...
the cancellation should be getting through any time now...

In article <15857 at csli.Stanford.EDU> poser at csli.stanford.edu (Bill Poser) writes:
>In article <2466 at ux.acs.umn.edu> edh at ux.acs.umn.edu (Eric D. Hendrickson) writes:
>>	char *duh = "Hello";
>>	printf("%s\n", duh);
>>	while (*duh <= strlen(duh)) {
>>		if (islower(*duh)) *duh = toupper(*duh);
>>		*duh++;
>>	}
>>      printf("%s\n",duh)
>
>The problem here is in the while termination condition. What this tests

There's more than just the one problem (that *duh will be > strlen(duh));

0.  *duh refers to the character, not the location;

1.  The loop changes the value of the pointer `duh', so it may print
nothing other than "O" once you get the loop to work;

2.  merely using (duh <= strlen(duh)) won't fix it; the
value of the pointer `duh' is almost certain to be larger
than strlen(duh).

I won't give fixes here; it's too instructive to work them out
yourself, especially at the apparent level of understanding.

>(An aside: since strlen(duh) never changes, either you or the compiler
>should move it outside the loop.) 

Trivial optimization compared to the massive bugs extant.

				--Blair
				  "I've been saying 'duh' myself
				   a lot, lately..."



More information about the Comp.lang.c mailing list