Should a compiler produce a warning/error for this?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MisterE

    Should a compiler produce a warning/error for this?

    typedef struct sg
    {
    int a;
    } G;

    int c(G* g)
    {
    return g->a;
    }

    int void(int argc, char **argv)
    {
    G g;
    c(g); //<----- instead of c(&g);
    }


  • MisterE

    #2
    Re: Should a compiler produce a warning/error for this?

    int void(int argc, char **argv)
    oops, err that should be int main(int argc, char **argv)



    Comment

    • Ian Collins

      #3
      Re: Should a compiler produce a warning/error for this?

      MisterE wrote:
      typedef struct sg
      {
      int a;
      } G;
      >
      int c(G* g)
      {
      return g->a;
      }
      >
      int void(int argc, char **argv)
      {
      G g;
      c(g); //<----- instead of c(&g);
      }
      >
      Yes, why would you think otherwise?

      --
      Ian Collins.

      Comment

      • Martin Ambuhl

        #4
        Re: Should a compiler produce a warning/error for this?

        MisterE wrote,hidden in the subject line, where it does not belong)
        "Should a compiler produce a warning/error for this?".
        Anything worth saying or asking is worth writing in the body of your
        message.
        typedef struct sg
        {
        int a;
        } G;
        int c(G* g)
        {
        return g->a;
        }
        int void(int argc, char **argv)
        ^^^^^^^^
        Yes, the compiler should issue a diagnostic for this.
        It makes any question about the body of the function irrelevant.
        {
        G g;
        c(g); //<----- instead of c(&g);
        }

        Comment

        • Martin Ambuhl

          #5
          Re: Should a compiler produce a warning/error for this?

          MisterE wrote:
          >int void(int argc, char **argv)
          >
          oops, err that should be int main(int argc, char **argv)
          Before "MisterE" just left the question out of the body of the text.
          This time he has destroyed any context at all, making the above
          incomprehensibl e.

          [Restored question and context]
          Subject: Should a compiler produce a warning/error for this?

          typedef struct sg
          {
          int a;
          } G;

          int c(G* g)
          {
          return g->a;
          }

          int main /* was: void */(int argc, char **argv)
          {
          G g;
          c(g); //<----- instead of c(&g);
          }

          [Answer]
          Of course the compiler should issue a diagnostic when the type of the
          argument is incompatible with the type of the parameter.
          If it doesn't, I suspect you have almost all diagnostics turned off.
          Turn them on! Now!

          Comment

          • MisterE

            #6
            Re: Should a compiler produce a warning/error for this?


            "Martin Ambuhl" <mambuhl@earthl ink.netwrote in message
            news:61nhe7F1uu atoU1@mid.indiv idual.net...
            MisterE wrote:
            >>int void(int argc, char **argv)
            >>
            >oops, err that should be int main(int argc, char **argv)
            >
            Before "MisterE" just left the question out of the body of the text. This
            time he has destroyed any context at all, making the above
            incomprehensibl e.
            >
            [Restored question and context]
            Subject: Should a compiler produce a warning/error for this?
            >
            typedef struct sg
            {
            int a;
            } G;
            >
            int c(G* g)
            {
            return g->a;
            }
            >
            int main /* was: void */(int argc, char **argv)
            {
            G g;
            c(g); //<----- instead of c(&g);
            }
            >
            [Answer]
            Of course the compiler should issue a diagnostic when the type of the
            argument is incompatible with the type of the parameter.
            If it doesn't, I suspect you have almost all diagnostics turned off.
            Turn them on! Now!
            The reason I asked is I have tried 3 compilers and so far none produced any
            sort of warning, at least in their default mode.
            If the variable isn't a structure, they will however.


            Comment

            • Ian Collins

              #7
              Re: Should a compiler produce a warning/error for this?

              MisterE wrote:
              >>
              >typedef struct sg
              >{
              > int a;
              >} G;
              >>
              >int c(G* g)
              >{
              > return g->a;
              >}
              >>
              >int main /* was: void */(int argc, char **argv)
              >{
              > G g;
              > c(g); //<----- instead of c(&g);
              >}
              >>
              >
              The reason I asked is I have tried 3 compilers and so far none produced any
              sort of warning, at least in their default mode.
              If the variable isn't a structure, they will however.
              >
              Care to name and shame? Whatever they are, they must be broken.

              --
              Ian Collins.

              Comment

              • Keith Thompson

                #8
                Re: Should a compiler produce a warning/error for this?

                "MisterE" <voids@sometwhe r.worldwrites:
                typedef struct sg
                {
                int a;
                } G;
                >
                int c(G* g)
                {
                return g->a;
                }
                >
                int void(int argc, char **argv)
                {
                G g;
                c(g); //<----- instead of c(&g);
                }
                In a later followup, MisterE wrote:
                oops, err that should be int main(int argc, char **argv)
                and later:
                The reason I asked is I have tried 3 compilers and so far none produced any
                sort of warning, at least in their default mode.
                If the variable isn't a structure, they will however.
                I don't doubt your sincerity, but that seems unlikely. In your
                initial article, you posted code with a serious syntax error ("int
                void" rather than "int main"), obviously a typo. That's no great sin
                (we all make mistakes) but it means that the code you posted is not
                exactly the same as the code that you actually compiled. You
                apparently re-typed the code, and perhaps paraphrased it, when you
                posted it here. That turns out to be a remarkably efficient way to
                change or delete the small detail that was actually causing whatever
                problem you were having.

                In my opinion, it's far more likely that the code you compiled without
                complaint on three different compilers was significantly different in
                some way from the code you posted, than that three different compilers
                would fail to catch the type mismatch in the function call.

                Please verify again that your code doesn't elicit a complaint, and
                then post the *exact* code that you actually compiled. Copy-and-paste
                it; don't paraphrase it, and don't re-type it.

                --
                Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                Nokia
                "We must do something. This is something. Therefore, we must do this."
                -- Antony Jay and Jonathan Lynn, "Yes Minister"

                Comment

                • Kenneth Brody

                  #9
                  Re: Should a compiler produce a warning/error for this?

                  Ian Collins wrote:
                  >
                  MisterE wrote:
                  >
                  typedef struct sg
                  {
                  int a;
                  } G;
                  >
                  int c(G* g)
                  {
                  return g->a;
                  }
                  >
                  int main /* was: void */(int argc, char **argv)
                  {
                  G g;
                  c(g); //<----- instead of c(&g);
                  }
                  >
                  The reason I asked is I have tried 3 compilers and so far none produced any
                  sort of warning, at least in their default mode.
                  If the variable isn't a structure, they will however.
                  Care to name and shame? Whatever they are, they must be broken.
                  Yes, please do.

                  Using MSVC, even with minimal warnings (-W0), I get this:

                  usenet.c
                  usenet.c(14) : error C2115: 'function' : incompatible types

                  Using gcc gives:

                  usenet.c: In function `main':
                  usenet.c:14: incompatible type for argument 1 of `c'

                  --
                  +-------------------------+--------------------+-----------------------+
                  | Kenneth J. Brody | www.hvcomputer.com | #include |
                  | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
                  +-------------------------+--------------------+-----------------------+
                  Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>


                  Comment

                  • =?UTF-8?q?Harald_van_D=C4=B3k?=

                    #10
                    Re: Should a compiler produce a warning/error for this?

                    On Sun, 17 Feb 2008 14:00:48 -0500, Kenneth Brody wrote:
                    Ian Collins wrote:
                    >MisterE wrote:
                    >>
                    >typedef struct sg
                    >{
                    > int a;
                    >} G;
                    >>
                    >int c(G* g)
                    >{
                    > return g->a;
                    >}
                    >>
                    >int main /* was: void */(int argc, char **argv) {
                    > G g;
                    > c(g); //<----- instead of c(&g);
                    >}
                    >>
                    >>
                    The reason I asked is I have tried 3 compilers and so far none
                    produced any sort of warning, at least in their default mode. If the
                    variable isn't a structure, they will however.
                    >
                    >Care to name and shame? Whatever they are, they must be broken.
                    >
                    Yes, please do.
                    >
                    Using MSVC, even with minimal warnings (-W0), I get this:
                    >
                    usenet.c
                    usenet.c(14) : error C2115: 'function' : incompatible types
                    >
                    Using gcc gives:
                    >
                    usenet.c: In function `main':
                    usenet.c:14: incompatible type for argument 1 of `c'
                    To add:

                    Using icc:
                    usenet.c(14): error: argument of type "G" is incompatible with parameter
                    of type "G *"
                    usenet.c(14): warning #592: variable "g" is used before its value is set

                    Using lcc:
                    Error /home/harald/usenet.c: usenet.c: 14 type error in argument 1 to
                    `c'; found 'struct sg' expected 'pointer to struct sg'

                    Using tendra: (significantly shortened)
                    [...]
                    Illegal conversion from type 'struct sg' to type 'struct sg *'.
                    [...]

                    Using tinycc:
                    usenet.c:14: cannot cast 'struct sg' to 'struct sg *'

                    Using Comeau online:
                    "ComeauTest .c", line 14: error: argument of type "G" is incompatible with
                    parameter of type "G *"
                    "ComeauTest .c", line 14: warning: variable "g" is used before its value
                    is set

                    Using pcc:
                    usenet.c, line 14: incompatible types for arg 1

                    Comment

                    • CBFalconer

                      #11
                      Re: Should a compiler produce a warning/error for this?

                      MisterE wrote:
                      "Martin Ambuhl" <mambuhl@earthl ink.netwrote
                      >MisterE wrote:
                      >>
                      >>>int void(int argc, char **argv)
                      >>>
                      >>oops, err that should be int main(int argc, char **argv)
                      >>
                      >Before "MisterE" just left the question out of the body of the
                      >text. This time he has destroyed any context at all, making the
                      >above incomprehensibl e.
                      >>
                      .... snip ...
                      >
                      The reason I asked is I have tried 3 compilers and so far none
                      produced any sort of warning, at least in their default mode.
                      If the variable isn't a structure, they will however.
                      Martins complaint was not about your question, it was about your
                      phrasing, failure to quote previous messages, and the general
                      incomprehensibi lity of your messages. ANY usenet message should be
                      understandable in isolation, without the subject line.

                      --
                      [mail]: Chuck F (cbfalconer at maineline dot net)
                      [page]: <http://cbfalconer.home .att.net>
                      Try the download section.



                      --
                      Posted via a free Usenet account from http://www.teranews.com

                      Comment

                      Working...