compiler reaction to wrong struct member name

M

migurus

OpenServer 5.0.7 with SCO OpenServer Development System (ver 5.2.0Aa)

I had a typo in my code referring to a struct member which does not
belong to the struct being used, but that name exists in a different
struct. Compiler did produce a warning and .o file, - I would think
this is a big nistake, not a warning level problem.

See example below:
$ cat c.c
typedef struct {
int a;
} N1;
typedef struct {
int a;
int b;
} N2;
main(int argc, char *argv[])
{
N1 n1;
N2 n2;
n1.a = 1;
n1.b = 1;
n2.a = 1;
n2.b = 1;
}
$ cc -c c.c
UX:i386acomp: WARNING: "c.c", line 13: warning: improper member use: b


GNU compiler worked correctly:
$ gcc -c c.c
c.c: In function `main':
c.c:13: structure has no member named `b'

and no .o file created.

So, question is, does anybody know if there is a setting or flag for
the compiler to NOT produce .o file for the bug described?

Just a note: I asked this question in comp.unix.sco.programmer, but
got no results there.
 
J

jacob navia

migurus a écrit :
OpenServer 5.0.7 with SCO OpenServer Development System (ver 5.2.0Aa)

I had a typo in my code referring to a struct member which does not
belong to the struct being used, but that name exists in a different
struct. Compiler did produce a warning and .o file, - I would think
this is a big nistake, not a warning level problem.

See example below:
$ cat c.c
typedef struct {
int a;
} N1;
typedef struct {
int a;
int b;
} N2;
main(int argc, char *argv[])
{
N1 n1;
N2 n2;
n1.a = 1;
n1.b = 1;
n2.a = 1;
n2.b = 1;
}
$ cc -c c.c
UX:i386acomp: WARNING: "c.c", line 13: warning: improper member use: b


GNU compiler worked correctly:
$ gcc -c c.c
c.c: In function `main':
c.c:13: structure has no member named `b'

and no .o file created.

So, question is, does anybody know if there is a setting or flag for
the compiler to NOT produce .o file for the bug described?

Just a note: I asked this question in comp.unix.sco.programmer, but
got no results there.

The sco compiler is not really a good one...

:)

Just forget it!
 
K

Keith Thompson

migurus said:
OpenServer 5.0.7 with SCO OpenServer Development System (ver 5.2.0Aa)

I had a typo in my code referring to a struct member which does not
belong to the struct being used, but that name exists in a different
struct. Compiler did produce a warning and .o file, - I would think
this is a big nistake, not a warning level problem.

See example below:
$ cat c.c
typedef struct {
int a;
} N1;
typedef struct {
int a;
int b;
} N2;
main(int argc, char *argv[])
{
N1 n1;
N2 n2;
n1.a = 1;
n1.b = 1;
n2.a = 1;
n2.b = 1;
}
$ cc -c c.c
UX:i386acomp: WARNING: "c.c", line 13: warning: improper member use: b


GNU compiler worked correctly:
$ gcc -c c.c
c.c: In function `main':
c.c:13: structure has no member named `b'

and no .o file created.

So, question is, does anybody know if there is a setting or flag for
the compiler to NOT produce .o file for the bug described?

That's a question about your compiler, not about the language. The
answer should be in your compiler's documentation (and it may be
"No").

As far as the language is concerned, your code violates a constraint,
which requires a diagnostic. The standard doesn't distinguish between
errors and warnings; either qualifies as a diagnostic. Paying close
attention to warnings, or using an option that makes warnings fatal
(if your compiler has such an option), should alleviate the problem.

Historically, member names were not restricted to the struct in which
they were defined. In early versions of C, a member name just
specified an offset and a type, without necessarily referring to a
particular struct type. Along with the lax rules on conversions
between addresses and integers, something like this:

struct {
int word0;
int word1;
};

int n = 0xabcd->word1;

might even have been legal. This was substantially tightened
up before K&R1 (1978), and even more so with the 1989 standard.
Your compiler might still have relics of ancient versions of the
language.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top