home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16113 < prev    next >
Encoding:
Text File  |  1992-11-08  |  3.9 KB  |  108 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!charon.amdahl.com!pacbell.com!ames!saimiri.primate.wisc.edu!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!destroyer!gumby!wupost!udel!sbcs.sunysb.edu!csws17.ic.sunysb.edu!rhorn
  3. From: rhorn@csws17.ic.sunysb.edu (Robert Horn)
  4. Subject: Re: Someone help me with this
  5. Message-ID: <1992Nov6.235237.25636@sbcs.sunysb.edu>
  6. Sender: usenet@sbcs.sunysb.edu (Usenet poster)
  7. Nntp-Posting-Host: csws17.ic.sunysb.edu
  8. Organization: State University of New York at Stony Brook
  9. References: <1992Nov6.210507.2887@vela.acs.oakland.edu>
  10. Date: Fri, 6 Nov 1992 23:52:37 GMT
  11. Lines: 95
  12.  
  13. In article <1992Nov6.210507.2887@vela.acs.oakland.edu> jmwojtal@vela.acs.oakland.edu (Wojo) writes:
  14. >I'm trying to do this program that goes into a void subroutine,
  15. >
  16. >void getday(char *, int, char *);
  17. >...
  18. >char fname[10];
  19. >int day;
  20. >char timetab[33][31];
  21. >
  22. >getday(fname, day, timetab);
  23. >...
  24. >void getday(char file[], int dy, char holdday[][]);
  25. >{
  26. >...
  27. >}
  28. >
  29. >Now it keeps giving me errors about different syntaxes.  
  30. >
  31. >Using C for the PC, what is the corrent way to apply this so it will work?
  32. >The subroutine is supposed to get values for timetab only;
  33. >
  34. >Thanks for the help
  35.  
  36. I'm not quite sure what you're trying to do (your three definitions/declarations
  37. of getday seem to all be mutually incompatable), however, if you use some
  38. typedefs you can clear up your otherwise muddy code:
  39.  
  40. typedef char timetab_t[33][31];  /*
  41.                   * Your prototype of getday would make one 
  42.                   * believe you want a simple string, 
  43.                   * it's called with an array of strings,
  44.                   * and the definition appears to want
  45.                   * an array of strings (any array of strings
  46.                   * of any length, a big no no [1])
  47.                   *
  48.                   * declare the type in a typedef so that
  49.                   * when you eventually figure out what
  50.                   * you want you can change it
  51.                   */
  52. void getday(char *filename, int some_int, timetab_t the_timetab);
  53.                  /*
  54.                   * the prototype appears before the call
  55.                   * (since a timetab_t is an array type
  56.                   * we don't need to declare it as a pointer)
  57.                   */
  58.  
  59. ...
  60.   char        fname[10];
  61.   int         day;
  62.   timetab_t   timetab;
  63.  
  64.   getday(fname, day, timetab)
  65.  
  66. ...
  67.  
  68.                  /*
  69.                   * be sure the types in the prototype
  70.                   * and the definition match exactly
  71.                   */
  72. void getday(char *filename, int day, timetab_t timetab) {
  73. ...
  74. }
  75.  
  76. [1] you cannot pass an arguement to a function such as:
  77.       return_type function(type identifier[][]) { }
  78.     since inside the function one will not know how far to index from 
  79.     identifier[0][0] to get to identifier[23][78], for example.
  80.     In C, the expression:
  81.       array[index]
  82.     is equivelent to:
  83.       *(array + index)
  84.     where adding index and array gives you a pointer index elements into
  85.     the array, using the sizeof an item in the array to determine what value
  86.     this pointer will have. since there is no bounds checking of any kind,
  87.     you don't need to know how long the array is, with a single dimmentioned
  88.     array. However, your example: char timetab[33][31] specifies an array from 0
  89.     to 30 of arrays 0 to 32 of characters.
  90.       timetab[i][j]
  91.     is equivelent to:
  92.       *(*(timetab + i) + j)
  93.     here you need to calculate the beginning of the ith element in timetab[],
  94.     which requires information on how large an element of timetab is. once you
  95.     have that, you can quickly find the jth element of the ith element of
  96.     timetab, since each element of the ith element of timetab is a character, and
  97.     has size 1. however, you need to know how big each element of timetab[] is
  98.     before you can do this, and C isn't about to give you this information, since
  99.     that would require passing the info around with the pointer which would be
  100.     icky and pascal like. You could, however, do:
  101.       void getday(char *fname, int blef, char spam[][31])
  102.     and this would be fine.
  103.  
  104.  
  105. -- 
  106. rhorn@ic.sunysb.edu           Never choose a college because it has a duckpond.
  107. Would you like to touch my wombat?          Send me hate mail, I love it.
  108.