MESSAGE
DATE | 2004-09-09 |
FROM | From: "Ruben"
|
SUBJECT | Subject: [hangout] Re: two dimensional arrays passed to functions
|
[This is an email copy of a Usenet post to "comp.lang.c"]
On Thu, 09 Sep 2004 14:32:51 -0400, Jens.Toerring wrote:
> Ruben wrote: >> On Thu, 09 Sep 2004 00:42:57 -0400, Ruben wrote: > >>> out of deperation because when I defined it as >>> >>> char * insert(char table[255],int cols, char values) >>> >>> I keep getting no data passed. >> >> Actually, I originally defined this function as char * insert(char >> table[255],int cols, char **values) > >> Everything I'm reading seems to say I have to define this as char * >> insert(char table[255],int cols, char values[][256]) > >> but main can be defined as int main (int argv, char **argc) without any >> problem. > >> This is driving me crazy because I could sware I did this on SCO >> compilers about 8 years ago without any trouble. > > Sorry, but the URL you gave for the code doesn't work.
Someone else said that you me but it seems to be working now. Covad was having some troubles over at tellhouse this morning
https://www.freedom-it.com/clinical.tgz is up currently
> The main question > is: what exactly is it that you want to pass to the function? From what > you write it looks as if in the caller you have the 'value' array > defined as > > char values[ N ][ 256 ]; > > where I don't know N but it doesn't really matter. If that is the case > then the form > > char * insert(char table[255],int cols, char values[][256]) > > looks reasonable.
But it's not what I originally wanted. I'm quite sure and relooked at old code on SCO and I used to just assign a char * to the address of the array and then send it down as a single dimmensional array. This method is dead appearently.
> The difference to the way argv is used is comes from > argv not being a 2-dimensional array but a 1-dimensional array of char > pointers. I.e. argv is defined similar to this > > char *x[ M ];
OK
> > Each of the M pointers may point to a different string.
x is a pointer to an array of chars.
> And that's not a > 2-dimensional array - it only has room for M char pointers and nothing > more.
But where is it allocated memory for the pointer address?
> All the strings pointed to must get their memory independently of > that definition >
Yes. I was using strcpy of string literals for that. In my main gtk program, they are coming from editable text objects.
> If you now pass something like argv to a function what the function gets > is a pointer to the first element of this array, i.e. a pointer to the > first pointer - that's why argv can be written as either "char **argv" > or "char *argv[ ]" in the call of main(). > > On the other hand > > char values[ N ][ 256 ]; >
> has room for N strings, each 256 chars long. And it does not get > converted to a pointer to pointer when passed to a function, since it's > not a 1-dimensional array (what the function actually gets can be > treated as a pointer to the first char in the first string, so you could > define the function as > > char * insert(char table[255],int cols, char *values)
I thought I tried this variation and it failed. I'll give it another run.
> > But that has the obvious drawback that then the compiler has no idea > where the strings start and you have to calculate that all by yourself, > i.e. the start of the n-th string would then be (value + n * 256) if you > start counting at 0. > Regards, Jens
Thanks
Ruben > > -- > \ Jens Thoms Toerring ___ Jens.Toerring-at-physik.fu-berlin.de > \__________________________ http://www.toerring.de ____________________________ NYLXS: New Yorker Free Software Users Scene Fair Use - because it's either fair use or useless.... NYLXS is a trademark of NYLXS, Inc
|
|