MESSAGE
DATE | 2004-09-09 |
FROM | Ruben Safir Secretary NYLXS
|
SUBJECT | Re: [hangout] Re: two dimensional arrays passed to functions
|
> The right way to do it: > > char * insert(char *table, int cols, char **values);
This just won't take in example codes or in the main program I'm writing. It compiles but segfaults. This is what I statrted with which has put me on 2 days of a twisted path.
In essence, everything I'm reading is saying that is you create a[3][255] then when you pass a as an argument, the data type sent is an array pointer
Additionally, strangley enough, what it needs is not the number of elements defined in the first index, but the size of the second index, otherwise it is clueless in the function to do poiter aritmitic.
http://www-users.cs.umn.edu/~tan/www-docs/C_lang.html
6.18: My compiler complained when I passed a two-dimensional array to a function expecting a pointer to a pointer.
A: The rule (see question 6.3) by which arrays decay into pointers is not applied recursively. An array of arrays (i.e. a two- dimensional array in C) decays into a pointer to an array, not a pointer to a pointer. Pointers to arrays can be confusing, and must be treated carefully; see also question 6.13. (The confusion is heightened by the existence of incorrect compilers, including some old versions of pcc and pcc-derived lints, which improperly accept assignments of multi-dimensional arrays to multi-level pointers.)
If you are passing a two-dimensional array to a function:
int array[NROWS][NCOLUMNS]; f(array);
the function's declaration must match:
f(int a[][NCOLUMNS]) { ... }
or
f(int (*ap)[NCOLUMNS]) /* ap is a pointer to an array */ { ... }
In the first declaration, the compiler performs the usual implicit parameter rewriting of "array of array" to "pointer to array" (see questions 6.3 and 6.4); in the second form the pointer declaration is explicit. Since the called function does not allocate space for the array, it does not need to know the overall size, so the number of rows, NROWS, can be omitted. The "shape" of the array is still important, so the column dimension NCOLUMNS (and, for three- or more dimensional arrays, the intervening ones) must be retained.
If a function is already declared as accepting a pointer to a pointer, it is probably meaningless to pass a two-dimensional array directly to it.
See also questions 6.12 and 6.15.
References: K&R1 Sec. 5.10 p. 110; K&R2 Sec. 5.9 p. 113; H&S Sec. 5.4.3 p. 126.
as an example of much of what I've read.
Even worse, it seems C's implementaitons for 2 dimensional arrarys is nothing for of inconsistent. Things I've read that I should be able to do, simple don't work.
Look at this sample program, for example:
#include #include #include #include
void print_str(char **b);
int main(int argv, char *argc[]){ char a[3][256];
strcpy(a[0], "Ruben"); strcpy(a[1], "Safir"); strcpy(a[2], "222222"); printf("\n%s", a[0]); printf("\n%s", a[1]); printf("\n%s", a[2]); print_str(a);
exit(0); }
void print_str( char **b ){ printf("\n%s", *b); b++; printf("\n%s", *b); b++; printf("\n%s\n", *b); return; }
ruben-at-flatbush:~> gcc -Wall test_func.c test_func.c: In function `main': test_func.c:21: warning: passing arg 1 of `print_str' from incompatible pointer type
It segfualts and according to the literature, it is the wrong pointer type because it is only a char *[255] and not a char**
I've tried a dozen variations on this to get it to work, defineing in main the d** and pointing it at a and at &a, etc. I've tried sending a[0] and changing the function definition to print_str( char *) and to call the function as print_str(a[0]) as recommended in the KN King book, which is an excellent C book, and this fails.
Even
char ** d; d = c;
or d = &c[0][0], etc have failed..............
> Advice: don't use arrays as arguments. It's stupid. Use pointers. >
I'm wishing I can take this advise! ____________________________ NYLXS: New Yorker Free Software Users Scene Fair Use - because it's either fair use or useless.... NYLXS is a trademark of NYLXS, Inc
|
|