home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!uknet!fulcrum!its!silver.its.bt.co.uk!jvt
- From: jvt@its.bt.co.uk (John Trickey)
- Newsgroups: comp.sys.atari.st.tech
- Subject: Re: A "C" question...
- Message-ID: <1992Jul30.165419.2528@its.bt.co.uk>
- Date: 30 Jul 92 16:54:19 GMT
- References: <16832DDF3.MDORMAN1@ua1vm.ua.edu> <1992Jul29.135300.12646@quando.quantum.de>
- Sender: @its.bt.co.uk
- Organization: BT Group Computing Unix Support, Birmingham, UK
- Lines: 67
-
- In article <1992Jul29.135300.12646@quando.quantum.de> vombruch@quando.quantum.de (Stefan vom Bruch) writes:
- >MDORMAN1@ua1vm.ua.edu (Mike Dorman) writes:
- >
- >: I've got a question about how to do something in "C".
-
- >: wx_name(ws,str)
- >: Window ws;
- >: char *str;
-
- Don't pass a struct on the stack (if typdef Window (strut window *) then
- OK). Its messy, slow and how do you return your results to the original
- struct as you're only working with a copy.
-
- >: I would then like to malloc() memory to hold the string that we've been given
-
- Yes I do this on memory intensive applications. Where strings are likely
- to be common, I save them in a struct for later searching and re-use.
- struct string {
- struct string *next;
- char *text;
- };
-
- Warning, for read-only strings and also **slow** but if you need the
- memory...
-
- > (as opposed to global variables). So when you call the function again
- > you can be sure that they have the same value as after the last call
- > of that function.
-
- Thats their use inside a function. The are also used inside a module at
- the outer level to define variables in the data seg that are not
- exported from that module. eg if you had a prog made up from
- main.c, mod1.c & mod2.c you could define
- static char *foo;
- in each and they would not interfere.
-
- > Static variables are very rare (on the atari anyway), and I don't
- > think you'll need them in this program.
-
- Sri, I beg to differ. They are a very valuable tool & you will find many
- library routines that return a char * from some other parameter(s) use
- the mechanism to store their string (otherwise they would not work). eg
-
- (char *)ctime((time_t *));
-
- That would be implemented as:
-
- char *
- fn(clock)
- time_t *clock;
- {
- static char result[LENGTH];
-
- sprintf(result, FORMAT_STRING, ARGS....);
- return result;
- }
-
- Hope this helps you understand what is going on.
-
- John
-
-
- --
- John Trickey <jvt@its.bt.co.uk>
- Voice: +44 21 524 8200 Fax: +44 21 553 6120
- (Home) <john@its.bt.co.uk> || <john@g4rev.ampr.org> || G4REV @ GB7FLG
- +44 21 308 8892
-