Pointer to array and gcc warnings

V

vippstar

Today I got a confusing message from gcc (I'm aware, those don't break
conformance ;-)

In function 'main':
7: warning: format '%d' expects type 'int', but argument 2 has type
'char (*)[1u]'

The code is

#include <stdio.h>

int main(void) {

char p[3][2];

printf("%d\n", &p[0]); /* this is errornous, but it's on purpose
there to generate the warning */

return 0;
}

I'm curious, what is gcc talking about?
Why is &p[0] a char (*)[1u]?

My first guess was that char (*)[N] pointers get promoted to char (*)
[1] pointers in VLA arguments. (it doesn't make much sense to me
though)

Can anyone explain (don't answer with a yes; actually do so :) gcc's
behavior here?
 
R

Richard

Today I got a confusing message from gcc (I'm aware, those don't break
conformance ;-)

In function 'main':
7: warning: format '%d' expects type 'int', but argument 2 has type
'char (*)[1u]'

The code is

#include <stdio.h>

int main(void) {

char p[3][2];

printf("%d\n", &p[0]); /* this is errornous, but it's on purpose
there to generate the warning */

return 0;
}

I'm curious, what is gcc talking about?
Why is &p[0] a char (*)[1u]?

My first guess was that char (*)[N] pointers get promoted to char (*)
[1] pointers in VLA arguments. (it doesn't make much sense to me
though)

Can anyone explain (don't answer with a yes; actually do so :) gcc's
behavior here?

"This is off topic in c.l.c. We do not know anything about gcc
here. please ask in a newsgroup more appropriate to your question. The
Gnu C newsgroup is down the corridor on the left."

Ah. That felt good.
 
K

Kenny McCormack

Today I got a confusing message from gcc (I'm aware, those don't break
conformance ;-)

In function 'main':
7: warning: format '%d' expects type 'int', but argument 2 has type
'char (*)[1u]'

The code is

#include <stdio.h>

int main(void) {

char p[3][2];

printf("%d\n", &p[0]); /* this is errornous, but it's on purpose
there to generate the warning */

return 0;
}

I'm curious, what is gcc talking about?
Why is &p[0] a char (*)[1u]?

My first guess was that char (*)[N] pointers get promoted to char (*)
[1] pointers in VLA arguments. (it doesn't make much sense to me
though)

Can anyone explain (don't answer with a yes; actually do so :) gcc's
behavior here?

"This is off topic in c.l.c. We do not know anything about gcc
here. please ask in a newsgroup more appropriate to your question. The
Gnu C newsgroup is down the corridor on the left."

Ah. That felt good.

Bathe in the glory. There's hope for you (*) yet.

(*) To get accepted into the he-man CLC regs society.
 
J

jameskuyper

Today I got a confusing message from gcc (I'm aware, those don't break
conformance ;-)

In function 'main':
7: warning: format '%d' expects type 'int', but argument 2 has type
'char (*)[1u]'

The code is

#include <stdio.h>

int main(void) {

char p[3][2];

printf("%d\n", &p[0]); /* this is errornous, but it's on purpose
there to generate the warning */

return 0;
}

I'm curious, what is gcc talking about?
Why is &p[0] a char (*)[1u]?

My first guess was that char (*)[N] pointers get promoted to char (*)
[1] pointers in VLA arguments. (it doesn't make much sense to me
though)

VLA normally refers to variable length arrays. You don't have any of
those in this program. I suspect you mean variable arguments. No such
promotion should occur.

As far as the C standard is concerned, the type of &p[0] is char(*)
[2]. Why gcc described it as char(*)[1u] is something you'll have to
take up with gcc. It's definitely version dependent. I have access to
gcc versions 3.2.3 and 3.3, both of which simply say:

warning: int format, pointer arg (arg 2)
 
V

vippstar

Today I got a confusing message from gcc (I'm aware, those don't break
conformance ;-)
In function 'main':
7: warning: format '%d' expects type 'int', but argument 2 has type
'char (*)[1u]'
The code is
#include <stdio.h>
int main(void) {
char p[3][2];
printf("%d\n", &p[0]); /* this is errornous, but it's on purpose
there to generate the warning */
return 0;
}
I'm curious, what is gcc talking about?
Why is &p[0] a char (*)[1u]?
My first guess was that char (*)[N] pointers get promoted to char (*)
[1] pointers in VLA arguments. (it doesn't make much sense to me
though)

VLA normally refers to variable length arrays. You don't have any of
those in this program. I suspect you mean variable arguments. No such
promotion should occur.

Whoops, I meant "variadic arguments".
As far as the C standard is concerned, the type of &p[0] is char(*)
[2]. Why gcc described it as char(*)[1u] is something you'll have to
take up with gcc. It's definitely version dependent. I have access to
gcc versions 3.2.3 and 3.3, both of which simply say:

warning: int format, pointer arg (arg 2)

Thanks. :)
 
F

Flash Gordon

Today I got a confusing message from gcc (I'm aware, those don't break
conformance ;-)
In function 'main':
7: warning: format '%d' expects type 'int', but argument 2 has type
'char (*)[1u]'
The code is
#include <stdio.h>
int main(void) {
char p[3][2];
printf("%d\n", &p[0]); /* this is errornous, but it's on purpose
there to generate the warning */
return 0;
}
I'm curious, what is gcc talking about?
Why is &p[0] a char (*)[1u]?
As far as the C standard is concerned, the type of &p[0] is char(*)
[2]. Why gcc described it as char(*)[1u] is something you'll have to
take up with gcc. It's definitely version dependent. I have access to
gcc versions 3.2.3 and 3.3, both of which simply say:

warning: int format, pointer arg (arg 2)

Thanks. :)

I suspect it was a bug that has been fixed. I get a far more useful warning:
markg@brenda:~$ gcc -ansi -pedantic -Wall -Wextra -g3 t.c
t.c: In function ‘main’:
t.c:7: warning: format ‘%d’ expects type ‘int’, but argument 2 has type
‘char (*)[2]’
markg@brenda:~$
 
V

vippstar

I suspect it was a bug that has been fixed. I get a far more useful warning:
markg@brenda:~$ gcc -ansi -pedantic -Wall -Wextra -g3 t.c
t.c: In function ‘main’:
t.c:7: warning: format ‘%d’ expects type ‘int’, but argument 2 has type
‘char (*)[2]’
markg@brenda:~$

Ah, thank you too.
For anyone interested, I'm using gcc 4.0.3
 
B

Barry Schwarz

Today I got a confusing message from gcc (I'm aware, those don't break
conformance ;-)

In function 'main':
7: warning: format '%d' expects type 'int', but argument 2 has type
'char (*)[1u]'

The code is

#include <stdio.h>

int main(void) {

char p[3][2];

printf("%d\n", &p[0]); /* this is errornous, but it's on purpose
there to generate the warning */

The & operator always results in a value of type pointer to something.
A pointer value is never the appropriate type of argument for %d. And
undefined behavior does break conformance.
return 0;
}

I'm curious, what is gcc talking about?
Why is &p[0] a char (*)[1u]?

p is an array of 3 array of 2 char.

p[0] is the first array of 2 char in the above object.

&p[0] is the address of p[0] with type pointer to array of 2 char. The
syntax for this type is char (*)[2].

The standard does not impose any quality requirements on the text of
diagnostics and this diagnostic is not even a required one. So what
you have is a useful message that happens to suffer from a slight
inaccuracy in terminology.
My first guess was that char (*)[N] pointers get promoted to char (*)
[1] pointers in VLA arguments. (it doesn't make much sense to me
though)

Can anyone explain (don't answer with a yes; actually do so :) gcc's
behavior here?
 

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,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top