Pointer to array and gcc warnings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vippstar@gmail.com

    Pointer to array and gcc warnings

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

    #2
    Re: Pointer to array and gcc warnings

    vippstar@gmail. com 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.

    Comment

    • Kenny McCormack

      #3
      Re: Pointer to array and gcc warnings

      In article <gbb32c$aai$3@r egistered.motza rella.org>,
      Richard <rgrdev@gmail.c omwrote:
      >vippstar@gmail .com 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.

      Comment

      • jameskuyper@verizon.net

        #4
        Re: Pointer to array and gcc warnings

        vipps...@gmail. com 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)

        Comment

        • vippstar@gmail.com

          #5
          Re: Pointer to array and gcc warnings

          On Sep 23, 7:13 pm, jameskuy...@ver izon.net wrote:
          vipps...@gmail. com 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. :-)

          Comment

          • Flash Gordon

            #6
            Re: Pointer to array and gcc warnings

            vippstar@gmail. com wrote, On 23/09/08 17:16:
            On Sep 23, 7:13 pm, jameskuy...@ver izon.net wrote:
            >vipps...@gmail .com 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 smap@spam.cause way.com
            If emailing me use my reply-to address
            See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

            Comment

            • vippstar@gmail.com

              #7
              Re: Pointer to array and gcc warnings

              On Sep 23, 10:06 pm, Flash Gordon <s...@spam.caus eway.comwrote:
              vipps...@gmail. com 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

              Comment

              • Barry Schwarz

                #8
                Re: Pointer to array and gcc warnings

                On Tue, 23 Sep 2008 08:40:58 -0700 (PDT), vippstar@gmail. com 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

                Comment

                Working...