home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.atari.st.tech
- Path: sparky!uunet!Cadence.COM!cadence.com!bammi
- From: bammi@acae127.cadence.com (Jwahar R. Bammi)
- Subject: Re: A "C" question...
- In-Reply-To: MDORMAN1@ua1vm.ua.edu's message of 28 Jul 92 20:46:58 GMT
- Message-ID: <BAMMI.92Jul31110621@acae127.cadence.com>
- Sender: usenet@Cadence.COM (Usenet News)
- Nntp-Posting-Host: acae127
- Organization: Cadence Design Systems
- References: <16832DDF3.MDORMAN1@ua1vm.ua.edu>
- Date: Sun, 19 Jul 1992 03:22:21 GMT
- Lines: 94
-
- In article <16832DDF3.MDORMAN1@ua1vm.ua.edu> MDORMAN1@ua1vm.ua.edu (Mike Dorman) writes:
-
- > be used. So I was thinking of creating functions wx_name() and wx_info(),
- > where the function definition would be something like:
- >
- > wx_name(ws,str)
- > Window ws;
- > char *str;
- >
- > I would then like to malloc() memory to hold the string that we've been given
- > so that we won't use any more memory than necessary (I have an ulterior motive,
- > I want to create a printf() interface to this stuff, too--I have a couple of
- > programs that could use it).
- >
- > So, what's the best way to declare the variable within the structure? Is
- > merely making it a char * sufficient, or do I need to make it static char *?
-
- ok. i'll bite: you really should be reading a good C book:
-
- you certainly do not want to make anything static inside a
- structure (in fact i am not sure if you even can). a static modifier
- just controls the scope of the declared object. for instance anything
- declared static at the module level (that is outside of any function)
- is visible only in the module (file) in which it appears. if a
- variable is declared static, then in addition to the scope semantics,
- it also makes it persistant. fields of a structure are not object/variables,
- so you cannot put the static modifier there.
-
- to answer your question: i guess the best way to optionally allocate
- some fileds is to declare the fields as pointers to the type you want,
- and malloc() them on demand. for instance
-
- typedef struct _window {
- int x,y,w,h;
- ....
- char *name;
- } *Window;
- #define WindowSize sizeof(struct _window)
-
- /* return an instance of Window */
- Window new_window()
- {
- Window w = (Window *) malloc(WindowSize);
-
- if(!w) fail();
-
- bzero(w, WindowSize);
- return w;
- }
-
- /* fill in the name field */
-
- wx_name(ws,str)
- Window ws;
- char *str;
- {
- if(!(ws->name = strdup(str)))
- fail();
- }
-
- Another way to approach this is to store the name in the structure
- itself.
-
- typedef struct _window {
- int x,y,w,h;
- ....
- char name[1];
- } *Window;
- #define WindowSize sizeof(struct _window)
-
- /* return an instance of Window */
- Window new_window(wname)
- char *wname;
- {
- Window w = (Window *) malloc(WindowSize + strlen(wname));
- /* In WindowSize we have accounted for 1 byte needed for the final \0
- needed to store the string , so we dont need strlen(wname)+1 above */
-
- if(!w) fail();
-
- bzero(w, WindowSize);
- strcpy(w->name, wname);
- return w;
- }
-
- in this scheme you can only initialize the name field when the window
- instance is created. the advantage is that you avoid the over head of
- one char pointer.
- --
- --
- bang: uunet!cadence!bammi jwahar r. bammi
- domain: bammi@cadence.com
- GEnie: J.Bammi
- CIS: 71515,155
-