Why Warning?

V

vashwath

Hi all,
My compiler is warning for passing a two dimensional character array to
a function which accepts "char**" as parameter.Can u please shed some
light on this issue?How can I get rid of this warning?
I am using Visual C for compiling.
Thanks
 
I

infobahn

Hi all,
My compiler is warning for passing a two dimensional character array to
a function which accepts "char**" as parameter.Can u please shed some
light on this issue?

We'll need some code to explain this, so let's write some.

#define M 10
#define N 12

void foo(char **);

int main(void)
{
char Names[M][N];
foo(Names); /* here's the problem - but why? see below */
return 0;
}

In C, a is equivalent to *(a + i), by definition.
Therefore, &a is equivalent to &*(a + i).
&* cancel, so &a is equivalent to (a + i).
Let i = 0.
So &a[0] is equivalent to (a + 0), and (a + 0) is just a.

In a value context (such as a function call), then, the name
of an array is interpreted as a pointer to the first element
of that array.

Names[0] is an array of N char.

So &Names[0] is a pointer to an array of N char. That is,
it has the type char (*)[N]. This is not the same type as
char **, which is "pointer to pointer to char" rather than
"pointer to array of N char".
How can I get rid of this warning?

One way would be to change the parameter type to char (*)[N].
If that isn't acceptable, you could re-design your array to be
an array of pointers to char, using dynamic allocation to get
the memory for the strings if need be.
 
C

Christian Bau

Hi all,
My compiler is warning for passing a two dimensional character array to
a function which accepts "char**" as parameter.Can u please shed some
light on this issue?How can I get rid of this warning?
I am using Visual C for compiling.

You should get a proper compiler that produces an error instead of a
warning. That is a problem with Visual C; it often allows you to compile
completely broken code and only gives a warning.

char** is a pointer to an array of pointers. A two dimensional array is
not an array of pointers, it is an array of arrays. So either the design
of the function interface is completely wrong, or you using it in a
completely wrong way.
 
J

Jack Klein

You should get a proper compiler that produces an error instead of a
warning. That is a problem with Visual C; it often allows you to compile
completely broken code and only gives a warning.

The C standard does not define "error" or "warning". It only
specifies "diagnostics" that are required in cases of syntax error or
constraint violation.
char** is a pointer to an array of pointers. A two dimensional array is
not an array of pointers, it is an array of arrays. So either the design
of the function interface is completely wrong, or you using it in a
completely wrong way.

This paragraph of your reply is completely true, but it is your first
paragraph that is broken, not "proper". The C standard that Microsoft
compilers adhere to NEVER forbids a translator to produce an
executable, no matter what is wrong with the source.
 
K

Kevin Bracey

In message <[email protected]>
Jack Klein said:
The C standard does not define "error" or "warning". It only
specifies "diagnostics" that are required in cases of syntax error or
constraint violation.

Yes, we know, but it's a quality of implementation thing, isn't it? If a
compiler doesn't distinguish between a blatantly incorrect piece of code that
violates an ISO constraint and something it thinks is just a little bit iffy,
how on earth is the average programmer supposed to know whether a "warning"
is important?

The last thing people who regard the C standard as important should be doing
is leaping to the defence of implementations like this, just to score a few
pedant points.
 

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
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top