From: rhorn@csws17.ic.sunysb.edu (Robert Horn)
Subject: Re: Someone help me with this
Organization: State University of New York at Stony Brook
Date: Fri, 6 Nov 1992 23:52:37 GMT

In article <1992Nov6.210507.2887@vela.acs.oakland.edu> jmwojtal@vela.acs.oakland.edu (Wojo) writes:
>I'm trying to do this program that goes into a void subroutine,
>
>void getday(char *, int, char *);
>...
>char fname[10];
>int day;
>char timetab[33][31];
>
>getday(fname, day, timetab);
>...
>void getday(char file[], int dy, char holdday[][]);
>{
>...
>}
>
>Now it keeps giving me errors about different syntaxes.  
>
>Using C for the PC, what is the corrent way to apply this so it will work?
>The subroutine is supposed to get values for timetab only;
>
>Thanks for the help

I'm not quite sure what you're trying to do (your three definitions/declarations
of getday seem to all be mutually incompatable), however, if you use some
typedefs you can clear up your otherwise muddy code:

typedef char timetab_t[33][31];  /*
				  * Your prototype of getday would make one 
				  * believe you want a simple string, 
				  * it's called with an array of strings,
				  * and the definition appears to want
				  * an array of strings (any array of strings
				  * of any length, a big no no [1])
				  *
				  * declare the type in a typedef so that
				  * when you eventually figure out what
				  * you want you can change it
				  */
void getday(char *filename, int some_int, timetab_t the_timetab);
				 /*
				  * the prototype appears before the call
				  * (since a timetab_t is an array type
				  * we don't need to declare it as a pointer)
				  */

...
  char        fname[10];
  int         day;
  timetab_t   timetab;

  getday(fname, day, timetab)

...

				 /*
				  * be sure the types in the prototype
				  * and the definition match exactly
				  */
void getday(char *filename, int day, timetab_t timetab) {
...
}

[1] you cannot pass an arguement to a function such as:
      return_type function(type identifier[][]) { }
    since inside the function one will not know how far to index from 
    identifier[0][0] to get to identifier[23][78], for example.
    In C, the expression:
      array[index]
    is equivelent to:
      *(array + index)
    where adding index and array gives you a pointer index elements into
    the array, using the sizeof an item in the array to determine what value
    this pointer will have. since there is no bounds checking of any kind,
    you don't need to know how long the array is, with a single dimmentioned
    array. However, your example: char timetab[33][31] specifies an array from 0
    to 30 of arrays 0 to 32 of characters.
      timetab[i][j]
    is equivelent to:
      *(*(timetab + i) + j)
    here you need to calculate the beginning of the ith element in timetab[],
    which requires information on how large an element of timetab is. once you
    have that, you can quickly find the jth element of the ith element of
    timetab, since each element of the ith element of timetab is a character, and
    has size 1. however, you need to know how big each element of timetab[] is
    before you can do this, and C isn't about to give you this information, since
    that would require passing the info around with the pointer which would be
    icky and pascal like. You could, however, do:
      void getday(char *fname, int blef, char spam[][31])
    and this would be fine.


-- 
rhorn@ic.sunysb.edu           Never choose a college because it has a duckpond.
Would you like to touch my wombat?		  Send me hate mail, I love it.
Newsgroups: comp.lang.c