Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

const et tab

6 views
Skip to first unread message

Patrick Pelissier

unread,
Jan 30, 2004, 8:27:18 AM1/30/04
to
Bonjour,
J'ai:

int f(const int tab[2][1])
{
return 0;
}

int g(void)
{
int tab[2][1];

return f(tab);
}

En compilant ce fichier, j'obtiens :
% gcc t.c -Wall -c -W -ansi -pedantic
t.c: In function `f':
t.c:3: warning: unused parameter `tab'
t.c: In function `g':
t.c:12: warning: passing arg 1 of `f' from incompatible pointer type

Je comprends parfaitement le premier avertissement, mais pas le deuxieme.
Pouvez-vous m'eclairer ?

Patrick

Emmanuel Delahaye

unread,
Jan 30, 2004, 12:01:47 PM1/30/04
to

Pour moi, et sous réserve que le code soit un copié/collé du tien, ce warning
n'est pas justifié.

Pour PCLint, non plus :

D:\CLC\P\Pelissie>\splint~1.6\bin\splint.exe -f \clc\borland.rc main.c
Splint 3.0.1.6 --- 11 Feb 2002

main.c(1,18): Function parameter tab declared as manifest array (size
constant is meaningless)
A formal parameter is declared as an array with size. The size of the array
is ignored in this context, since the array formal parameter is treated as a
pointer. (Use -fixedformalarray to inhibit warning)

Finished checking --- 1 code warning

--
-ed- emdelY...@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

Horst Kraemer

unread,
Jan 30, 2004, 12:49:18 PM1/30/04
to

Le paramčtre a le type

const int (*) [1]

(pointeur vers tableau d'un const int)


et l'argument a le type

int (*) [1]

(pointeur vers tableau d'un int)


Selon la norme il n'y a pas de conversion automatique entre ces types.
Il faut un cast

return f ( (const int(*)[1])tab );

--
Horst

Emmanuel Delahaye

unread,
Jan 30, 2004, 1:11:46 PM1/30/04
to
In 'fr.comp.lang.c', Horst Kraemer <horst....@epost.de> wrote:

>> int f(const int tab[2][1])
>> {
>> return 0;
>> }
>>
>> int g(void)
>> {
>> int tab[2][1];
>>
>> return f(tab);
>> }
>>

>> t.c: In function `g':
>> t.c:12: warning: passing arg 1 of `f' from incompatible pointer type
>>

> Le paramètre a le type


>
> const int (*) [1]
>
> (pointeur vers tableau d'un const int)
>
>
> et l'argument a le type
>
> int (*) [1]
>
> (pointeur vers tableau d'un int)
>
>
> Selon la norme il n'y a pas de conversion automatique entre ces types.
> Il faut un cast

J'aurais dit le contraire... Mais je ne t'ai jamais vu commetre d'erreur ici,
alors je suis dubitatif...

> return f ( (const int(*)[1])tab );

Pour moi, un pointeur sur const peut recevoir la valeur d'un pointeur. C'est
à cause des 2 dimensions que ça pose problème?

Vincent Lefevre

unread,
Jan 30, 2004, 7:45:59 PM1/30/04
to
Dans l'article <Xns9480C3461D1...@213.228.0.4>,
Emmanuel Delahaye <emdelY...@noos.fr> écrit:

> > return f ( (const int(*)[1])tab );

> Pour moi, un pointeur sur const peut recevoir la valeur d'un
> pointeur. C'est à cause des 2 dimensions que ça pose problème?

Oui, apparemment. Avec une dimension, aucun problème.

--
Vincent Lefèvre <vin...@vinc17.org> - Web: <http://www.vinc17.org/> - 100%
validated (X)HTML - Acorn Risc PC, Yellow Pig 17, Championnat International
des Jeux Mathématiques et Logiques, TETRHEX, etc.
Work: CR INRIA - computer arithmetic / SPACES project at LORIA

Horst Kraemer

unread,
Jan 31, 2004, 2:42:54 PM1/31/04
to
On 30 Jan 2004 18:11:46 GMT, Emmanuel Delahaye <emdelY...@noos.fr>
wrote:

>In 'fr.comp.lang.c', Horst Kraemer <horst....@epost.de> wrote:
>
>>> int f(const int tab[2][1])
>>> {
>>> return 0;
>>> }
>>>
>>> int g(void)
>>> {
>>> int tab[2][1];
>>>
>>> return f(tab);
>>> }
>>>
>>> t.c: In function `g':
>>> t.c:12: warning: passing arg 1 of `f' from incompatible pointer type
>>>
>> Le paramètre a le type
>>
>> const int (*) [1]
>>
>> (pointeur vers tableau d'un const int)
>>
>>
>> et l'argument a le type
>>
>> int (*) [1]
>>
>> (pointeur vers tableau d'un int)
>>
>>
>> Selon la norme il n'y a pas de conversion automatique entre ces types.
>> Il faut un cast
>
>J'aurais dit le contraire... Mais je ne t'ai jamais vu commetre d'erreur ici,
>alors je suis dubitatif...
>
>> return f ( (const int(*)[1])tab );
>
>Pour moi, un pointeur sur const peut recevoir la valeur d'un pointeur. C'est
>à cause des 2 dimensions que ça pose problème?

La norme dit qu'il y a une conversion automatique

toto * p
vers
const|volatile toto * p

cela ne s'étend ni à (1)

toto ** p
vers
const|volatile toto ** p

et ni à (2)

toto (*p) [N]
vers
const|volatile toto (*p)[N]


On peut montrer que (1) ne serait pas sûre,

(
const char cc[] = "hello";

void f( const char **cpp)
{
*cpp = cc; /* OK */
}

char *p;
f(&p); /* ??? */
p[0] = 'H'; /* comportement indéfini, f a fait pointer
p vers char const */

)

mais apparamment (2) serait sûre mais la norme y tient - peut-etre
pour des raisons formelles.

--
Horst

0 new messages