Should a compiler produce a warning/error for this?

M

MisterE

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);
}
 
I

Ian Collins

MisterE said:
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?
 
M

Martin Ambuhl

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.
 
M

Martin Ambuhl

MisterE said:
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!
 
M

MisterE

Martin Ambuhl said:
MisterE said:
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.
 
C

CBFalconer

MisterE said:
.... 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.
 
K

Keith Thompson

MisterE said:
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);
}

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.
 
K

Kenneth Brody

Ian said:
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:[email protected]>
 
H

Harald van Dijk

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top