S
subramanian100in
Consider the following program:
#include <stdio.h>
void myfn(const int **a)
{
static int i, j, k;
a[0] = &i;
a[1] = &j;
a[2] = &k;
return;
}
int main(void)
{
int *a[3];
int x, y, z;
a[0] = &x;
a[1] = &y;
a[2] = &z;
x = 10;
y = 20;
z = 30;
myfn(a);
return 0;
}
Suppose the program name is tmp.c
When this program is compiled with gcc under Redhat Enerprise Linux,
with the command
gcc -std=c99 tmp.c
the following Compilation warning is produced with gcc
const_ptr.c: In function `main':
const_ptr.c:27: warning: passing arg 1 of `myfn' from incompatible
pointer type
If I remove the const qualifier in myfn( ), the program compiles with
gcc without any warning.
However with VC++ 2005, there is no compilation warning with the
original program ie even when the const qualifier is present.
QUESTION:
-----------------
Why is the warning generated with gcc ?
This question arises because of the following reason.
We can pass a char[] to a function which receives it as const char*.
Here there is no compilation warning reported. But when a char *a[] is
passed, why can't it be received as "const char ** " ?
#include <stdio.h>
void myfn(const int **a)
{
static int i, j, k;
a[0] = &i;
a[1] = &j;
a[2] = &k;
return;
}
int main(void)
{
int *a[3];
int x, y, z;
a[0] = &x;
a[1] = &y;
a[2] = &z;
x = 10;
y = 20;
z = 30;
myfn(a);
return 0;
}
Suppose the program name is tmp.c
When this program is compiled with gcc under Redhat Enerprise Linux,
with the command
gcc -std=c99 tmp.c
the following Compilation warning is produced with gcc
const_ptr.c: In function `main':
const_ptr.c:27: warning: passing arg 1 of `myfn' from incompatible
pointer type
If I remove the const qualifier in myfn( ), the program compiles with
gcc without any warning.
However with VC++ 2005, there is no compilation warning with the
original program ie even when the const qualifier is present.
QUESTION:
-----------------
Why is the warning generated with gcc ?
This question arises because of the following reason.
We can pass a char[] to a function which receives it as const char*.
Here there is no compilation warning reported. But when a char *a[] is
passed, why can't it be received as "const char ** " ?