Passing pointer array to function

T

Tatu Portin

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
 
E

E. Robert Tisdale

Tatu said:
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

It seems to compile just fine for me.
 
M

Mike Wahler

Tatu Portin said:
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

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
 
J

Joe Wright

Tatu said:
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

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.
 
T

Tatu Portin

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;
 
R

Raymond Martineau

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.
 
C

Chris Torek

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.
 
C

CBFalconer

Mike said:
Why not?


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.
 
C

Chris Torek

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.
 
M

Mike Wahler

CBFalconer said:
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
 
T

Tatu Portin

Tatu said:
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')
 
A

Andrey Tarasevich

Tatu said:
...
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-lite/const-correctness.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.
 

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,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top