Comple error when passing two dimensional char arrays as parameters

Z

ZMan

The following code won't compile with gcc version 3.4.2
(mingw-special).
How come?
Error: cannot convert `char (*)[80]' to `char**'

/**********************************************************/
#include <cstdio>

#define MAX_WORD_LEN 80
#define MAX_SIZE 1000

void test (char* argv[]);

int main(int argc, char* argv[])
{
char words[MAX_SIZE][MAX_WORD_LEN];
test( words );
return 0;
}

void test (char* argv[])
{
}
/**********************************************************/
 
M

Martin Ambuhl

ZMan said:
The following code won't compile with gcc version 3.4.2
(mingw-special).
How come?
Error: cannot convert `char (*)[80]' to `char**'

/**********************************************************/
#include <cstdio>

C++ code is properly addressed in <comp.lang.c++>.
If you rewrite your code as C, invoke gcc as a C compiler (including
using the correct filename extension), and still have a problem, get
back to us.
 
P

Peter Shaggy Haywood

Groovy hepcat ZMan was jivin' on 15 Nov 2006 23:43:48 -0800 in
comp.lang.c.
Comple error when passing two dimensional char arrays as parameters's
a cool scene! Dig it!
The following code won't compile with gcc version 3.4.2
(mingw-special).
How come?
Error: cannot convert `char (*)[80]' to `char**'

Uh huh. As soon as I saw your subject line I knew what the problem
was. You're trying to pass a 2D array to a function expecting a
pointer to pointer. This is a mistake. Pointers and arrays are not the
same thing. This causes more confusion than a mouse at a burlesque
show. But it's really quite simple.
An array is converted to a pointer to its first element when passed
to a function. I'm sure you are aware of that. But you have to take
into account what it's an array of. An array of int is converted to a
pointer to int. An array of char is converted to a pointer to char. An
array of struct foo is converted to a pointer to struct foo. And so
on. Now, a 2D array is essentially an array of array. Thus, when
passed to a function it is converted to a pointer to array.
#include <cstdio>

What on Earth is that? There ain't no sech animal in standard C.
Anyhow, you're not calling any library functions, so you don't need
any headers.
#define MAX_WORD_LEN 80
#define MAX_SIZE 1000

void test (char* argv[]);

Here test() is declared as taking a pointer to pointer to char. I
know it looks like an array of pointer to char, but of course that
can't be so. It's a pointer to pointer to char.
int main(int argc, char* argv[])

Since you're not using any of main()'s parameters, you can use the
parameterless version:

int main(void)
{
char words[MAX_SIZE][MAX_WORD_LEN];

Here you declare an array of array of char.
test( words );

Now you pass that array of array of char to test(), and it is
converted to a pointer to array of char. But that won't do because
test() wants a pointer to pointer to char.
return 0;
}

void test (char* argv[])
{
}

Solution: you need to change test() to accept a pointer to array of
char.

void test(char (*argv)[MAX_WORD_LEN]);

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top