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

  1. Newsgroups: comp.sys.atari.st.tech
  2. Path: sparky!uunet!Cadence.COM!cadence.com!bammi
  3. From: bammi@acae127.cadence.com (Jwahar R. Bammi)
  4. Subject: Re: A "C" question...
  5. In-Reply-To: MDORMAN1@ua1vm.ua.edu's message of 28 Jul 92 20:46:58 GMT
  6. Message-ID: <BAMMI.92Jul31110621@acae127.cadence.com>
  7. Sender: usenet@Cadence.COM (Usenet News)
  8. Nntp-Posting-Host: acae127
  9. Organization: Cadence Design Systems
  10. References: <16832DDF3.MDORMAN1@ua1vm.ua.edu>
  11. Date: Sun, 19 Jul 1992 03:22:21 GMT
  12. Lines: 94
  13.  
  14. In article <16832DDF3.MDORMAN1@ua1vm.ua.edu> MDORMAN1@ua1vm.ua.edu (Mike Dorman) writes:
  15.  
  16. > be used.  So I was thinking of creating functions wx_name() and wx_info(),
  17. > where the function definition would be something like:
  18. >  
  19. > wx_name(ws,str)
  20. > Window ws;
  21. > char *str;
  22. >  
  23. > I would then like to malloc() memory to hold the string that we've been given
  24. > so that we won't use any more memory than necessary (I have an ulterior motive,
  25. > I want to create a printf() interface to this stuff, too--I have a couple of
  26. > programs that could use it).
  27. >  
  28. > So, what's the best way to declare the variable within the structure?  Is
  29. > merely making it a char * sufficient, or do I need to make it static char *?
  30.  
  31. ok. i'll bite: you really should be reading a good C book:
  32.  
  33.     you certainly do not want to make anything static inside a
  34. structure (in fact i am not sure if you even can). a static modifier
  35. just controls the scope of the declared object. for instance anything
  36. declared static at the module level (that is outside of any function)
  37. is visible only in the module (file) in which it appears. if a
  38. variable is declared static, then in addition to the scope semantics,
  39. it also makes it persistant. fields of a structure are not object/variables,
  40. so you cannot put the static modifier there.
  41.  
  42. to answer your question: i guess the best way to optionally allocate
  43. some fileds is to declare the fields as pointers to the type you want,
  44. and malloc() them on demand. for instance
  45.  
  46. typedef struct _window {
  47.    int    x,y,w,h;
  48.    ....
  49.    char *name;
  50. } *Window;
  51. #define WindowSize sizeof(struct _window)
  52.  
  53. /* return an instance of Window */
  54. Window new_window()
  55. {
  56.      Window w = (Window *) malloc(WindowSize);
  57.  
  58.      if(!w)    fail();
  59.  
  60.      bzero(w, WindowSize);
  61.      return w;
  62. }
  63.  
  64. /* fill in the name field */
  65.  
  66. wx_name(ws,str)
  67. Window ws;
  68. char *str;
  69. {
  70.     if(!(ws->name = strdup(str)))
  71.     fail();
  72. }
  73.  
  74. Another way to approach this is to store the name in the structure
  75. itself.
  76.  
  77. typedef struct _window {
  78.    int    x,y,w,h;
  79.    ....
  80.    char name[1];
  81. } *Window;
  82. #define WindowSize sizeof(struct _window)
  83.  
  84. /* return an instance of Window */
  85. Window new_window(wname)
  86. char *wname;
  87. {
  88.      Window w = (Window *) malloc(WindowSize + strlen(wname));
  89. /* In WindowSize we have accounted for 1 byte needed for the final \0
  90.    needed to store the string , so we dont need strlen(wname)+1 above */
  91.  
  92.      if(!w)    fail();
  93.  
  94.      bzero(w, WindowSize);
  95.      strcpy(w->name, wname);
  96.      return w;
  97. }
  98.  
  99. in this scheme you can only initialize the name field when the window
  100. instance is created. the advantage is that you avoid the over head of
  101. one char pointer.
  102. --
  103. --
  104. bang:   uunet!cadence!bammi            jwahar r. bammi
  105. domain: bammi@cadence.com
  106. GEnie:    J.Bammi
  107. CIS:    71515,155
  108.