home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.os.ms-windows.programmer.misc
- Path: sparky!uunet!mcsun!sunic!kth.se!dront.nada.kth.se!d88-jwa
- From: d88-jwa@dront.nada.kth.se (Jon WΣtte)
- Subject: Re: difference between STUFF *stuff & STUFF stuff[]???
- Message-ID: <1993Jan6.151741.29382@kth.se>
- Sender: usenet@kth.se (Usenet)
- Nntp-Posting-Host: dront.nada.kth.se
- Organization: Royal Institute of Technology, Stockholm, Sweden
- References: <1993Jan6.084522.25921@emr1.emr.ca>
- Date: Wed, 6 Jan 1993 15:17:41 GMT
- Lines: 71
-
- In <1993Jan6.084522.25921@emr1.emr.ca> jagrant@emr1.emr.ca (John Grant) writes:
-
- > static STUFF stufftable[MAXSTUFF];
- > and in another module that referenced it:
- > extern STUFF stufftable[];
- >Ok, that was fine and everything worked ok.
-
- I'm not sure that it should. Since stufftable is explicitly
- static to a module (file) you shouldn't have gotten at it
- from anywhere else. If you did, that's a bug.
-
- > static STUFF *stufftable;
- > stufftable=malloc(MAXSTUFF*sizeof(STUFF));
- >and in the other module, I left it as:
- > extern STUFF stufftable[];
-
- This of course is totally bogus. It tells the compiler that
- you have a data area consisting of items, and that saying
- "stufftable" will generate the address of that data area.
-
- > extern STUFF *stufftable;
- >and re-built, it ran perfectly.
-
- Well, this is the correct thing, saying there's this variable
- stufftable pointing to data items, and saying "stufftable"
- gives you the contents of that variable.
-
- However, since stufftable is static in the first module, you
- should not have been able to get at it; the linker should
- have given you an unresolved external error.
-
- >and explain why it is so dangerous here, but it's perfectly ok
- >to use a pointer or a [] in a function declaration.
-
- I did, above. Better yet, buy Kernighan & Ritchie,
- "The C Programming Language" which explain this and a lot
- of other things about C.
-
- One common and recommended way of avoiding this kind of
- problems is to have the extern declaration in a header,
- that both the defining and using file uses.
-
- foo.h:
-
- extern fooItem * fooPointer ;
-
-
- foo.c:
-
- #include "foo.h"
-
- fooItem * fooPointer = NULL ;
-
- void
- InitFoo ( void )
- {
- fooPointer = ( fooItem * ) calloc ( sizeof ( fooItem ) , 47 ) ;
- }
-
-
- bar.c:
-
- #include "foo.h"
-
- ...
- fooPointer [ 4 ] = ...
-
- --
- -- Jon W{tte, h+@nada.kth.se, Mac Hacker Deluxe --
-
- Cookie Jar: Vanilla Yoghurt with Crushed Oreos.
-