Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!sundc!pitstop!sun!amdcad!ames!hc!beta!hwe From: hwe@beta.UUCP (Skip Egdorf) Newsgroups: comp.lang.c Subject: Re: Question on structures Message-ID: <11782@beta.UUCP> Date: Sun, 1-Nov-87 17:16:48 EST Article-I.D.: beta.11782 Posted: Sun Nov 1 17:16:48 1987 Date-Received: Thu, 5-Nov-87 21:08:32 EST References: <1025@phoenix.Princeton.EDU> Organization: Los Alamos Natl Lab, Los Alamos, N.M. Lines: 57 Keywords: Structures records Summary: pascal 'with' in C In article <1025@phoenix.Princeton.EDU>, asjoshi@phoenix.Princeton.EDU (Amit S. Joshi) writes: > Hi, > > I have a question on structures. It is as follows: > > I have gotten really tired of typing things like a.b.c.d etc to refer to > elements of a structure. I remember that pascal provided me with a 'with' > construction which allowed me to say something like > "with a.b.c {now refering to d really means use a.b.c.d } ..." > I was wondering if there is some similar contruct in C. I might be missing > something obvious, I wonder if somebody could help me ?. I know I could of > course make a local copy in the program of every variable in the structures > and then use the local copy but that seems inefficient and would be quite > unclear. > -- > Amit Joshi | BITNET : Q3696@PUCC.BITNET > | USENET : ...seismo!princeton!phoenix!asjoshi This question arises around here once in a while, and this is how I answer, for what it is worth. No, there is no equivalent to 'with' in C. However, your question is symptomatic of a deeper problem; You seem to be trying to write Pascal programs in C. Every language has a 'style' that encourages good programs in some way. Many programmers have seen Fortran programs written in PL/I that are awfull (structured control constructs not used, etc.). Lisp programmers have all seen at least one Fortran program written with lots of parens. (setq ...) (setq ...) (setq ...) (setq ...) (write ...) The point is that attempting to use one language's style with a different language usually produces a poor product. Pascal forces one into a style with all variables, types, and consts global at the highest level. Some block structure can be used, but many things must be global. This leads to a record being defined at a high level, and used with 'dot' notation throughout the entire program. This leads to 'a.b.c.d' references and the 'with' construct to cut down on the verbage. In C, it is much more common for the particular piece of the record (now a struct) being processed by some sub procedure, with the sub structure ('d' in a.b.c.d) being passed as an address. This leads to a style of several small routines dealing with 'struct a-b-c' pointers, and most references of the form 'p->d'. Neither of these styles is gospel, to be found everywhere always. The programmer, as always, is responsable for picking the best tool for the job at hand. However, the C style of dealing with deep structure references will usually fit best within C programs, and 'with' usage will fit best in most Pascal programs. Trying to use either in the wrong place will usually result in a clumsy construct in the program. Skip Egdorf hwe@lanl.gov