By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,865 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,865 IT Pros & Developers. It's quick & easy.

Comple error when passing two dimensional char arrays as parameters

P: n/a
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[])
{
}
/************************************************** ********/

Nov 16 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ZMan wrote:
The following code won't compile with gcc version 3.4.2
(mingw-special).
How come?
Error: cannot convert `char (*)[80]' to `char**'
See the FAQ (http://c-faq.com) section 6.18

--
Ian Collins.
Nov 16 '06 #2

P: n/a
ZMan wrote:
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.
Nov 17 '06 #3

P: n/a
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"?
Nov 18 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.