Pointer to array and gcc warnings

Discussion in 'C Programming' started by vippstar@gmail.com, Sep 23, 2008.

  1. Guest

    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?
    , Sep 23, 2008
    #1
    1. Advertising

  2. Richard Guest

    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.
    Richard, Sep 23, 2008
    #2
    1. Advertising

  3. In article <gbb32c$aai$>,
    Richard <> wrote:
    > 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.
    Kenny McCormack, Sep 23, 2008
    #3
  4. Guest

    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)
    , Sep 23, 2008
    #4
  5. Guest

    On Sep 23, 7:13 pm, wrote:
    > 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. :)
    , Sep 23, 2008
    #5
  6. Flash Gordon Guest

    wrote, On 23/09/08 17:16:
    > On Sep 23, 7:13 pm, wrote:
    >> 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
    If emailing me use my reply-to address
    See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
    Flash Gordon, Sep 23, 2008
    #6
  7. Guest

    On Sep 23, 10:06 pm, Flash Gordon <> wrote:

    > 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
    , Sep 23, 2008
    #7
  8. On Tue, 23 Sep 2008 08:40:58 -0700 (PDT), 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
    Barry Schwarz, Sep 25, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charlie Zender
    Replies:
    12
    Views:
    1,077
    Peter Pichler
    Jan 3, 2004
  2. Charlie Zender

    Removing GCC compiler warnings from fabsf(), sqrtf()...

    Charlie Zender, Jan 2, 2004, in forum: C Programming
    Replies:
    8
    Views:
    622
    Ben Pfaff
    Jan 4, 2004
  3. Warnings with gcc

    , Nov 22, 2005, in forum: C Programming
    Replies:
    34
    Views:
    1,373
    Flash Gordon
    Nov 25, 2005
  4. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    449
    James Kuyper
    Sep 23, 2011
  5. Ted Sung
    Replies:
    1
    Views:
    307
    Sherm Pendley
    Aug 30, 2004
Loading...

Share This Page