Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Pointer to array and gcc warnings

Reply
Thread Tools

Pointer to array and gcc warnings

 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      09-23-2008
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?
 
Reply With Quote
 
 
 
 
Richard
Guest
Posts: n/a
 
      09-23-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> 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.
 
Reply With Quote
 
 
 
 
Kenny McCormack
Guest
Posts: n/a
 
      09-23-2008
In article <gbb32c$aai$(E-Mail Removed)>,
Richard <(E-Mail Removed)> wrote:
>(E-Mail Removed) writes:
>
>> 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.

 
Reply With Quote
 
jameskuyper@verizon.net
Guest
Posts: n/a
 
      09-23-2008
(E-Mail Removed) wrote:
> 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)
 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      09-23-2008
On Sep 23, 7:13 pm, (E-Mail Removed) wrote:
> (E-Mail Removed) wrote:
> > 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.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      09-23-2008
(E-Mail Removed) wrote, On 23/09/08 17:16:
> On Sep 23, 7:13 pm, (E-Mail Removed) wrote:
>> (E-Mail Removed) wrote:
>>> 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]?


<snip>

>> 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:~$
--
Flash Gordon
If spamming me sent it to (E-Mail Removed)
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      09-23-2008
On Sep 23, 10:06 pm, Flash Gordon <(E-Mail Removed)> wrote:

> (E-Mail Removed) wrote:
> > Today I got a confusing message from gcc (I'm aware, those don't break
> > conformance


<snip>

> 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
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      09-25-2008
On Tue, 23 Sep 2008 08:40:58 -0700 (PDT), (E-Mail Removed) wrote:

>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?


--
Remove del for email
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Array of pointer and pointer of array erfan C Programming 6 01-28-2008 08:55 PM
use warnings; and use Warnings; give different results Ted Sung Perl Misc 1 08-30-2004 10:22 PM
Removing GCC compiler warnings from fabsf(), sqrtf()... Charlie Zender C Programming 8 01-04-2004 01:11 AM
Vexing GCC warnings: "assignment discards qualifiers from pointertarget type" Charlie Zender C Programming 12 01-03-2004 12:18 AM



Advertisments