Departments


We Have Mail


Dear Mr. Plauger [via e-mail],

I recently read your editorial in The C Users Journal in which you expressed the fact that the bad letters that you get affect you every bit as much as the good letters.

Having been a net.resident for a few years, I can relate to the sting that someone's flame can bring. Well, I intend for this letter to be the opposite of one of those flames.

Although just a hobbiest and weekend hack, I've been at it long enough to remember Whitesmiths and Software Tools when it was a new book. I have enjoyed your writing style since those days and continue to enjoy it to this day.

In fact, I have recently been forced to let some of my many magazine subscriptions lapse, and noticed that all of the magazines that I continue to receive (except Byte) have you as either the editor or a contributing editor. When I recycle the magazines, I clip out your articles to save and refer to later; nothing else in them holds the timeless appeal that your writing does.

This is not to say that I agree with everything you say. For example, I hold the idealistic viewpoint of free software that only someone who hasn't been forced to live on his own handiwork can hold. I understand your view, but I guess I just long for a more utopian world. And I'll never flame you for stating your viewpoint.

Some folks though, see the net and its brethren as a forum for their ability to write hate mail. It is amazing how the most innocent of statements can raise the ire of these small thinking people. My postings usually consist of a simple question or comment — and I always draw a flame or two (or...). You, on the other hand, must make statements that almost guarantee a dissenting viewpoint. You are thus guaranteed an almost limitless supply of flames.

Don't let it get you down. Sometimes (seldom though) the bastards actually have something to say once you filter out the hate and anger.

And besides, it's summer where you are! Thumb your nose at those idiots in the States! Enjoy!

Well anyway, this letter was meant to give you a nice warm fuzzy and I hope it did. If it didn't, feel free to flame me :-)

Keith Walker
8710 Kelly Ct.
Spokane, WA 99208

Thanks, I needed that. Unfortunately, I left Sydney summer to return to New England winter. Luckily, I like cold weather. pjp

Mr. Plauger,

Seems like there should be something besides simply 'Mr.' in front of your name like 'Your Cness' or 'Most High C' or something. I truly enjoy your column in the CUJ, I have learned a great deal from reading and studying your column. I have been a C hack for almost 10 years now and continue to learn something new all the time, especially since I started teaching C at the university level. I try to teach ANSI C and have had to unlearn some of my old coding habits — your column, Rex's column, and some of Tom Plum's books have been an immense help.

Anyway my purpose here is to ask a question:

1   struct test
2   {
3       int num1;
4       float num2;
5   } stru, *stru_ptr;
6
7      char string[] = "num1";
8
9      stru_ptr = &stru;
10
11     stru_ptr->num1 = 5;
What I would like to know is there some method to evaluate string so that I can use it on line 11 to access the desired structure member i.e.

stru_ptr->(string) = 5;
I am reading an ASCII file header that has a member name and a type for the data values in the file. A linked list is created from the header (since headers from different files have variable lengths) keeping the name and the type of each field in the data portion of the file. As the data portion of the file is read each field is converted according to the appropriate type from the linked list member. What I would like to do is use the name portion from the list to access the appropriate structure member and assign the data value.

Can I do it?

Thanks for your help. Keep up the excellent work.

Pat McClanahan
mcclanah@dlgec.cr.usgs.gov

I faced much the same problem reading locale files in The Standard C Library. See the chapter on the header <locale.h>. The basic trick is to use the offsetof macro to locate each field:

#include <stdlib.h>

struct x {
   const char *name;
   size_t offset; } fields[] = {
   {"num1", offsetof(struct x, num1)},
   ...};
If fields[i].name matches, and you know its type is int, you can write the rather horrid expression:

*(int *)((char *)&stru + fields[i].offset) = 5;
It's not a graceful solution, but it is portable. Hope this helps. —pip

Dear C Users Journal,

I hope you can help me track down an article. I was told an article exists somewhere where an individual published the source code for a driver to make a joystick emulate a mouse. The ultimate goal was to use the Atarii Power Glove. The article was to have been published in the last 12 months. I have not found it yet. If you can tell me where to find the article or if you have Turbo C or assembly source code for a joystick to mouse device driver please inform me of where to find it.

Second a friend of mine received The Point Editor free with the purchase of his mouse from Logitech. When I purchased the same mouse package the editor was not available. The editor is awesome for C! I have tried to purchase the editor and had no luck. Did Logitech sell the product rights to someone? Why was the product pulled. You can't beat it. Can the source code be purchased?

Thank you.

Richard Grabbe
R.B. #3 Box 56
Owensburg, IN 47453

Berney Williams (head of Book Publishing) replies:

The only article I could locate was "Of Mice and Messages Event Driven Programming in C," Dr. Dobbs Journal, March 1, 1991.

The C Users Group contains CUG321, Mousetrap Library, described in the August 1990 CUG New Releases column; and CUG354, CES Mouse Tools Library with Joystick, described in the November 1991 CUG New Releases column. Call Customer Relations to place an order.

Dear Editor,

I am a C programmer and a lapsed subscriber to your magazine. It is possible that I will soon begin to use C to process criminological statistics, both through an IBM PC (Turbo C 2.0) and through an IBM mainframe (CMS/MVS). I have no background in statistics whatsoever. I realize that there are very fine statistical packages already available, such as SAS and SPSS, but the idea of staying in the driver's seat and writing my own formulas appeals to me strongly. Accordingly, I have a couple of questions for you.

(1) Does your Journal have an index of all past articles available on diskette in ASCII or plain-text format that I can search on PC or upload to mainframe? In any format at all? Or even in hardcopy? I am specifically interested in articles pertaining to C and statistics.

(2) I would like to know if anyone has available a tutorial—even a correspondence course—that teaches introductory statistical concepts, preferably vis-a-vis the C programming language. Ideally, I would like to be able to write my own statistical formulas in C.

Sincerely,

Lee H. Hill
1010 Walkup Street
Carbondale, IL 62901

Berney Williams (head of Book Publishing) replies:

CUJ has not published anything on statistics. We are developing a machine-readable index to post issues. An index through 1990 is running on our inhouse XENIX system now, and we are indexing 1991. We plan to offer a cumulative index in versions for UNIX/XENIX, DOS, and Windows.

Dear Mr. Plauger,

Regarding Bob van der Poel's global variable problem in the February 1992 issue. Bob obviously desires to have his code not only technically correct but stylistically elegant, therefore he is not really satisfied with any of the solutions offered regarding the most elegant way of handling global declarations. After writing a lot of lines of C code beset with global variable problems, I realized that the best way to achieve elegant style with global variables is to not use them at all! To say, "I want elegant style with my global variables" is a little bit like saying "I want to drive a really luxurious automobile, but it has to be just like the 1971 Toyota Corolla that I've always driven."

The solution to global variables that I like, is to put them all in structures, then pass the structures around, not global variable definitions. Need to add or delete a variable? Modify the structure. (Don't forget to recompile everything.) This effectively keeps all code modules "ignorant" of the information that the other's need, but makes it available if it's needed later. This also nicely eliminates the problem of forgetting to change every declaration of a global variable in multiple source files. There is one place to change it — in the include file where it is declared.

About the only time I use global variables NOW is when I have completely messed up the design and don't want to go back and clean it up, or an operating system error trap that I can't pass anything to. Then I like to use a global pointer to a structure.

I usually wind up having one structure for the particular program I'm designing that contains pointers to other structures as needed. Programming then usually involves writing routines that do something to or with a structure or array of structures. The "something" is either filling them in, processing the information in them, or using the results. In fact, by the time I design the structures I have pretty much designed the program, it becomes obvious what needs to be done to get the results I want.

Hey, I have a great idea. Why don't we define a computer language that make's working with structures easier, doing a lot of the rudimentary initializations, assignments, and the like? We could give it a name that indicates how it augments C, like, oh, I don't know, maybe, C PLUS PLUS! ! !???

Naw, that's too scary, and I wonder if Toyota still makes the Corolla?

David Johns
R/D Manager
Market Strategies, Inc.
333 S.W. 5th Ave.
Portland, OR 97204

Dumping an assortment of variables into a single structure is an old COBOL programmers' trick. Larry Constantine labeled it "stamp coupling" and warned of its dangers. From a maintenance standpoint, it's almost as bad as the "pathological coupling" you get using global variables.

I think you're right that there's no good solution to these problems. Any time you have to share data in ways that ignore the function-call hierarchy, you have problems. You can ameliorate the problems, but you can't make them go away. pip

Dear Editor,

Please note that in Sydney S. Weinstein's "On The Networks" column entitled "Where To Get The Sources" in the February 1992 edition of The C Users Journal, my last name is spelled incorrectly as "Kames;" the correct spelling is "Kamens."

Sincerely,

Jonathan I. Kamens
11 Ashford Terrace
Allston, MA 02134

Dear Dr. Plauger,

In your March 1992 issue, James Cook wrote about his problem with the getch function in his curses library running under VMS. He wanted keystrokes to return immediately as does the getch function on a PC. You answered that such functions are almost universal in appearance but not in name. You suggested that Mr. Cook hide his machine-dependent code in a getraw function.

I cannot speak for Mr. Cook's version of curses under VMS; however standard BSD and System V curses provide either (or both) the cbreak and crmode functions which, in the words of the manual:

"...characters typed by the user are immediately available to the program...Most interactive programs using curses will set CBREAK mode."

I think that every curses program that I have seen calls either the cbreak or crmode function. Mr. Cook is right — it is nice to have keystrokes returned without needing to press the return/enter key. That's one of the main reasons why I wrote a curses program — to get immediate handling of keystrokes as well as screen handling in a machine-independent fashion.

Andy Levinson
The Institute OSM Ltd.
11575 Sunshine Terrace
Studio City, CA 91604-3835

Dear Dr. Plauger,

You mentioned (in the April 1992 issue) that the logic is easy to get wrong...could it possibly be that you meant

const size_t pivot = n >> 1;
instead of

const size_t pivot = n > 1; /* ??? */
my testing for a project some time back showed >> instead of /= 2 runs almost twice as fast (total time spent in bsearch that is, not just the expression), but the src for the UNIX I use (sysv.3.2.2) uses the divide for unknown reasons.

David X Callaway

I got the logic right for bsearch (even though I botched qsort in the first printing of the book). The error you spotted is an artifact of Ventura publisher. The "greater than" sign is magic and I forgot to escape it properly here. Thanks for pointing out the error. pjp

Dear Mr. Plauger,

I have just sent in my check for renewal and I would like to to know that I really enjoy your magazine. I would also like to propose a solution to the problem of code disk distribution. I run a public BBS out of a High School in NYC, we are currently getting money to receive Internet Mail Feeds. If you like, we would be happy to serve as a host for CUJ.

Regarding Barry Thomas's letter in the July 1991 issue, I do have a copy of core wars and would be happy to mail him a copy but he did not leave an I-net address. You can reach me at any of these adresses:

DQZ5052@ACFcluster.nyu.edu
epw@timessqr.gc.cuny.edu

I look forward to your response.

Jarett D. Chaiken