home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / os / mswindo / programm / misc / 4628 < prev    next >
Encoding:
Text File  |  1993-01-06  |  2.0 KB  |  55 lines

  1. Newsgroups: comp.os.ms-windows.programmer.misc
  2. Path: sparky!uunet!spool.mu.edu!sdd.hp.com!usc!cs.utexas.edu!torn!nott!emr1!jagrant
  3. From: jagrant@emr1.emr.ca (John Grant)
  4. Subject: difference between STUFF *stuff & STUFF stuff[]???
  5. Message-ID: <1993Jan6.084522.25921@emr1.emr.ca>
  6. Organization: Energy, Mines, and Resources, Ottawa
  7. Date: Wed, 6 Jan 1993 08:45:22 GMT
  8. Lines: 45
  9.  
  10. At the risk of appearing stupid again, I have another (perhaps
  11. obvious, but not right now at 3:30 AM) question...
  12.  
  13. I had the following in my app:
  14.    in my global module:    #define MAXSTUFF 10
  15.             static STUFF stufftable[MAXSTUFF];
  16.    and in another module that referenced it:
  17.             extern STUFF stufftable[];
  18. Ok, that was fine and everything worked ok.
  19.  
  20. I decided to change it to a heap table instead, so I declared
  21. it in global as:
  22.             static STUFF *stufftable;
  23. and at startup time, allocated it:
  24.             stufftable=malloc(MAXSTUFF*sizeof(STUFF));
  25. and in the other module, I left it as:
  26.             extern STUFF stufftable[];
  27.  
  28. I thought that it was ok (actually I forgot to change it), because
  29. you can use things like
  30.         somefunc(...,STUFF *x,...);
  31.     and    somefunc(...,STUFF x[],...);
  32. interchangeably in function calling sequences, i.e. there is
  33. no difference between a pointer and the address of an array.
  34.  
  35. Boy, was I surprised!  When I re-built and tested it, all hell
  36. broke loose, with UAE and crashes left, right & centre.  Note
  37. that I always enable every single warning when I compile and it
  38. didn't tell me there was anything wrong.  Anyway, when I changed
  39. the reference to:
  40.             extern STUFF *stufftable;
  41. and re-built, it ran perfectly.
  42.  
  43. So, can someone tell me the difference between the following 2
  44. statements:
  45.     extern STUFF *stufftable;
  46.     extern STUFF stufftable[];
  47. with respect to the code generated when I reference stufftable, i.e.:
  48.     stufftable[i].xxx=...
  49. and explain why it is so dangerous here, but it's perfectly ok
  50. to use a pointer or a [] in a function declaration.
  51. -- 
  52. John A. Grant                        jagrant@emr1.emr.ca
  53. Airborne Geophysics
  54. Geological Survey of Canada, Ottawa
  55.