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

Passing pointer array to function

P: n/a
I have a struct:

typedef struct {
char **user_comments;
int *comment_wds;
int comments;
char *vendor;
} vorbis_comment;
void func (char **table)
{
return;
}

int main (void)
{
vorbis_comment vc;
/* This part GCC doesn't like */

func (vc.user_comments);

return 0;
}

GCC (3.1) would say that
warning: passing arg 1 of 'func' from incompatible pointer type


But still my code in 'func' (not posted, above is an example) works correctly,
utilizing whole '**table'.

Now what I want to ask is that which is the legal way to make a call of this
kind? Or is it just a bug or "feature" of GCC?
I am compiling with a commandline:

gcc -Wall -pedantic -ansi -mcpu=athlon -ffast-math -O2
Nov 14 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a

"Tatu Portin" <ax****@mbnet.fi> wrote in message
news:TE***************@read3.inet.fi...
I have a struct:

typedef struct {
char **user_comments;
int *comment_wds;
int comments;
char *vendor;
} vorbis_comment;
void func (char **table)
{
return;
}

int main (void)
{
vorbis_comment vc;
/* This part GCC doesn't like */

func (vc.user_comments);

return 0;
}

GCC (3.1) would say that
warning: passing arg 1 of 'func' from incompatible pointer type
But still my code in 'func' (not posted,


Why not?
above is an example) works correctly,
utilizing whole '**table'.

Now what I want to ask is that which is the legal way to make a call of this kind? Or is it just a bug or "feature" of GCC?


We cannot tell you the correct form for calling a function whose
definition (at least declaration) that we cannot see. What is
the signature of 'func()'? Remember that an array is not a pointer,
and a pointer is not an array.

-Mike
Nov 14 '05 #2

P: n/a
Tatu Portin wrote:
I have a struct:

cat main.c typedef struct {
char** user_comments;
int* comment_wds;
int comments;
char* vendor;
} vorbis_comment;
void func(char** table) {
return;
}

int main(int argc, char* argv[]) {
vorbis_comment vc;

/* GCC doesn't like this part. */

func(vc.user_comments);

return 0;
}
gcc -Wall -ansi -pedantic -ffast-math -O2 -o main main.c
gcc --version gcc (GCC) 3.4.1

GCC (3.1) would say that
>warning: passing arg 1 of 'func' from incompatible pointer type


It seems to compile just fine for me.
Nov 14 '05 #3

P: n/a
Tatu Portin wrote:
I have a struct:

typedef struct {
char **user_comments;
int *comment_wds;
int comments;
char *vendor;
} vorbis_comment;
void func (char **table)
{
return;
}

int main (void)
{
vorbis_comment vc;
/* This part GCC doesn't like */

func (vc.user_comments);

return 0;
}

GCC (3.1) would say that
>warning: passing arg 1 of 'func' from incompatible pointer type


But still my code in 'func' (not posted, above is an example) works
correctly, utilizing whole '**table'.

Now what I want to ask is that which is the legal way to make a call of
this kind? Or is it just a bug or "feature" of GCC?
I am compiling with a commandline:

gcc -Wall -pedantic -ansi -mcpu=athlon -ffast-math -O2


Your code exactly as written here, pasted to tatu.c and compiled with ..

gcc -Wall -pedantic -ansi -mcpu=athlon -ffast-math -O2 tatu.c

... compiles here without error.

C:\work\c\clc>gcc --version
gcc.exe (GCC) 3.1
Copyright (C) 2002 Free Software Foundation, Inc.

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #4

P: n/a
Ok. First post was wrong. (I thought that simplifying would do the same. It
didn't.)

Here we are:
42:int fprint_comments_formatted
43: ( FILE *tg
44: , const vorbis_comment *vc)
45:{
46: register int i;

....

69: if (flag) {

70: entry = has_str (s_album, vc->user_comments, vc->comments);

71: val = strpbrk (vc->user_comments[entry], "=");
72: val++;
73: fprintf (tg, "%s\n", val);
74:
75: flag = 0;
76: }

....

90: return 0;
91:}

gcc -Wall -pedantic -ansi -mcpu=athlon -ffast-math -O2 cdmaker.c cdmaker_1.o -o
cdmaker.exe
cdmaker.c In function 'fprint_comments_formatted':
cdmaker.c:70 warning: passing arg 2 of 'has_str' from incompatible pointer type
Function prototypes:
int fprint_comments_formatted
( FILE *tg
, const vorbis_comment *vc);

int has_str
( const char *str
, const char **table
, int ent); /* Number of entries in '**table' */
Struct definition:
typedef struct {
char **user_comments;
int *comment_wds;
int comments;
char *vendor;
} vorbis_comment;
Nov 14 '05 #5

P: n/a
On Sun, 12 Dec 2004 03:43:16 GMT, Tatu Portin <ax****@mbnet.fi> wrote:
Ok. First post was wrong. (I thought that simplifying would do the same. It
didn't.)

Here we are:
42:int fprint_comments_formatted
43: ( FILE *tg
44: , const vorbis_comment *vc)
45:{
46: register int i;

...

69: if (flag) {

70: entry = has_str (s_album, vc->user_comments, vc->comments);


You might get a bit more help compiling it with a C++ compiler:

E:\temp>gpp -Wall a.cpp
a.cpp: In function `int main()':
a.cpp:27: error: invalid conversion from `char**' to `const char**'
a.cpp:27: error: initializing argument 2 of `int has_str(const char*,
const char**, int)'

It's valid in C, but GCC decides it's worth raising a warning about
converting 'char**' to 'const char**'. You can get rid of the warning by
doing a type cast, but there may be better methods of dealing with it.
Nov 14 '05 #6

P: n/a
In article <o7***********@read3.inet.fi> Tatu Portin <ax****@mbnet.fi> wrote:
Ok. First post was wrong. (I thought that simplifying would do the same. It
didn't.)


Always test the simplified-for-post version. :-)

Without quoting a lot, your problem is the "const" qualifier,
which does not work right in C. Stop using it, and the problem
will go away (this is not necessarily the best solution, but is
certainly the easiest to describe).

See also the comp.lang.c FAQ, question 11.10.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #7

P: n/a
Mike Wahler wrote:

"Tatu Portin" <ax****@mbnet.fi> wrote in message
news:TE***************@read3.inet.fi...
I have a struct:

typedef struct {
char **user_comments;
int *comment_wds;
int comments;
char *vendor;
} vorbis_comment;
void func (char **table)
{
return;
}

int main (void)
{
vorbis_comment vc;
/* This part GCC doesn't like */

func (vc.user_comments);

return 0;
}

GCC (3.1) would say that
warning: passing arg 1 of 'func' from incompatible pointer type


But still my code in 'func' (not posted,


Why not?
above is an example) works correctly,
utilizing whole '**table'.

Now what I want to ask is that which is the legal way to make a call of

this
kind? Or is it just a bug or "feature" of GCC?


We cannot tell you the correct form for calling a function whose
definition (at least declaration) that we cannot see. What is
the signature of 'func()'? Remember that an array is not a pointer,
and a pointer is not an array.


Er - he gave the full definition of func. What he didn't do was
initialize vc, which should not cause a compile error.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #8

P: n/a
"CBFalconer" <cb********@yahoo.com> wrote in message
news:41***************@yahoo.com...
Mike Wahler wrote:

We cannot tell you the correct form for calling a function whose
definition (at least declaration) that we cannot see. What is
the signature of 'func()'? Remember that an array is not a pointer,
and a pointer is not an array.


Er - he gave the full definition of func. What he didn't do was
initialize vc, which should not cause a compile error.


Um, yes, it seems that it's me who can't see today. :-)

-Mike
Nov 14 '05 #9

P: n/a
In article <8e********************************@4ax.com>
Raymond Martineau <bk***@ncf.ca> wrote:
You might get a bit more help compiling it with a C++ compiler:

E:\temp>gpp -Wall a.cpp
a.cpp: In function `int main()':
a.cpp:27: error: invalid conversion from `char**' to `const char**'
a.cpp:27: error: initializing argument 2 of `int has_str(const char*,
const char**, int)'

It's valid in C ...
Actually, it is *not* valid in C either.
but GCC decides it's worth raising a warning about converting
'char**' to 'const char**'.
GCC simply chooses to complain-and-keep-going in this case, rather
than complain-and-stop-compiling. GCC calls the former a "warning"
and the latter an "error", but the C standard says only that the
conversion is incorrect and must elicit a "diagnostic".

The set of things-that-cause-stopping is different for GCC's C
compiler than for GCC's C++ compiler, partly because they are
maintained by different people, partly because C compilers have
traditionally accepted all kinds of invalid source and generated
machine code anyway (which promptly core-dumps, in many cases),
and of course partly because the languages are different (although
in this case, the semantics actually match up, for once).
You can get rid of the warning by doing a type cast, but there may
be better methods of dealing with it.


In C++, if you made has_str() take a "const char *const *" parameter,
the problem would go away, but it would remain a problem in C.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #10

P: n/a
Tatu Portin wrote:
Ok. First post was wrong. (I thought that simplifying would do the same.
It didn't.)

Here we are:
42:int fprint_comments_formatted
43: ( FILE *tg
44: , const vorbis_comment *vc)
45:{
46: register int i;

....

69: if (flag) {

70: entry = has_str (s_album, vc->user_comments, vc->comments);

71: val = strpbrk (vc->user_comments[entry], "=");
72: val++;
73: fprintf (tg, "%s\n", val);
74:
75: flag = 0;
76: }

....

90: return 0;
91:}

gcc -Wall -pedantic -ansi -mcpu=athlon -ffast-math -O2 cdmaker.c
cdmaker_1.o -o cdmaker.exe
cdmaker.c In function 'fprint_comments_formatted':
cdmaker.c:70 warning: passing arg 2 of 'has_str' from incompatible
pointer type
Function prototypes:
int fprint_comments_formatted
( FILE *tg
, const vorbis_comment *vc);

int has_str
( const char *str
, const char **table
, int ent); /* Number of entries in '**table' */
Struct definition:
typedef struct {
char **user_comments;
int *comment_wds;
int comments;
char *vendor;
} vorbis_comment;

Thank you all. The problem was, as you said, the const qualifier.

But is anyone capable of saying why exactly this fails? (with 'const char **table')
Nov 14 '05 #11

P: n/a
Tatu Portin wrote:
...
But is anyone capable of saying why exactly this fails? (with 'const char **table')
...


The reason why type 'T**' is not implicitly convertible to type 'const
T**' (in both C and C++) is explained well in the C++ FAQ

http://www.parashift.com/c++-faq-lit...html#faq-18.15

This is C++ FAQ, but the reasoning applies immediately to C as well. In
short, this conversion, if it was allowed, would create a large hole in
the "wall" of const-correctness checks :) Sometimes it might appear to
be illogical though.

It might be worth noting that C++ allows implicit 'T**' -> 'const T*
const*' conversion. C doesn't.

--
Best regards,
Andrey Tarasevich

Nov 14 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.