home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / sys / atari / st / tech / 4198 < prev    next >
Encoding:
Text File  |  1992-07-30  |  2.4 KB  |  79 lines

  1. Path: sparky!uunet!mcsun!uknet!fulcrum!its!silver.its.bt.co.uk!jvt
  2. From: jvt@its.bt.co.uk (John Trickey)
  3. Newsgroups: comp.sys.atari.st.tech
  4. Subject: Re: A "C" question...
  5. Message-ID: <1992Jul30.165419.2528@its.bt.co.uk>
  6. Date: 30 Jul 92 16:54:19 GMT
  7. References: <16832DDF3.MDORMAN1@ua1vm.ua.edu> <1992Jul29.135300.12646@quando.quantum.de>
  8. Sender: @its.bt.co.uk
  9. Organization: BT Group Computing Unix Support, Birmingham, UK
  10. Lines: 67
  11.  
  12. In article <1992Jul29.135300.12646@quando.quantum.de> vombruch@quando.quantum.de (Stefan vom Bruch) writes:
  13. >MDORMAN1@ua1vm.ua.edu (Mike Dorman) writes:
  14. >
  15. >: I've got a question about how to do something in "C".
  16.  
  17. >: wx_name(ws,str)
  18. >: Window ws;
  19. >: char *str;
  20.  
  21. Don't pass a struct on the stack (if typdef Window (strut window *) then
  22. OK). Its messy, slow and how do you return your results to the original
  23. struct as you're only working with a copy.
  24.  
  25. >: I would then like to malloc() memory to hold the string that we've been given
  26.  
  27. Yes I do this on memory intensive applications. Where strings are likely
  28. to be common, I save them in a struct for later searching and re-use.
  29. struct string {
  30.     struct string *next;
  31.     char *text;
  32. };
  33.  
  34. Warning, for read-only strings and also **slow** but if you need the
  35. memory...
  36.  
  37. >    (as opposed to global variables). So when you call the function again    
  38. >    you can be sure that they have the same value as after the last call
  39. >    of that function.
  40.  
  41. Thats their use inside a function. The are also used inside a module at
  42. the outer level to define variables in the data seg that are not
  43. exported from that module. eg if you had a prog made up from
  44. main.c, mod1.c & mod2.c you could define
  45.     static char *foo;
  46. in each and they would not interfere.
  47.  
  48. >    Static variables are very rare (on the atari anyway), and I don't
  49. >    think you'll need them in this program.
  50.  
  51. Sri, I beg to differ. They are a very valuable tool & you will find many
  52. library routines that return a char * from some other parameter(s) use
  53. the mechanism to store their string (otherwise they would not work). eg
  54.  
  55.     (char *)ctime((time_t *));
  56.  
  57. That would be implemented as:
  58.  
  59. char *
  60. fn(clock)
  61. time_t *clock;
  62. {
  63.     static char result[LENGTH];
  64.  
  65.     sprintf(result, FORMAT_STRING, ARGS....);
  66.     return result;
  67. }
  68.  
  69. Hope this helps you understand what is going on.
  70.  
  71. John
  72.  
  73.  
  74. -- 
  75. John Trickey <jvt@its.bt.co.uk>
  76.        Voice: +44 21 524 8200     Fax: +44 21 553 6120
  77. (Home) <john@its.bt.co.uk> || <john@g4rev.ampr.org> || G4REV @ GB7FLG
  78.        +44 21 308 8892
  79.