Should a compiler produce a warning/error for this?

Discussion in 'C Programming' started by MisterE, Feb 16, 2008.

  1. MisterE

    MisterE Guest

    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, Feb 16, 2008
    #1
    1. Advertising

  2. MisterE

    MisterE Guest


    > int void(int argc, char **argv)


    oops, err that should be int main(int argc, char **argv)
     
    MisterE, Feb 16, 2008
    #2
    1. Advertising

  3. MisterE

    Ian Collins Guest

    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.
     
    Ian Collins, Feb 16, 2008
    #3
  4. 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);
    > }
     
    Martin Ambuhl, Feb 16, 2008
    #4
  5. 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
    incomprehensible.

    [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!
     
    Martin Ambuhl, Feb 16, 2008
    #5
  6. MisterE

    MisterE Guest

    "Martin Ambuhl" <> wrote in message
    news:...
    > 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
    > incomprehensible.
    >
    > [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.
     
    MisterE, Feb 16, 2008
    #6
  7. MisterE

    CBFalconer Guest

    MisterE wrote:
    > "Martin Ambuhl" <> wrote
    >> 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 incomprehensible.
    >>

    .... 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
    incomprehensibility 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
     
    CBFalconer, Feb 17, 2008
    #7
  8. "MisterE" <> writes:
    > 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_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 17, 2008
    #8
  9. 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:>
     
    Kenneth Brody, Feb 17, 2008
    #9
  10. 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
     
    Harald van Dijk, Feb 17, 2008
    #10
    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. vengala s reddy
    Replies:
    0
    Views:
    1,935
    vengala s reddy
    Mar 5, 2004
  2. Rhino
    Replies:
    14
    Views:
    7,520
    Chris Uppal
    Dec 14, 2005
  3. Replies:
    5
    Views:
    466
    Luc The Perverse
    Oct 27, 2006
  4. Angus
    Replies:
    33
    Views:
    8,317
  5. Dann Corbit
    Replies:
    7
    Views:
    976
    Jorgen Grahn
    Jul 13, 2012
Loading...

Share This Page