scope and life of tagged structure

R

Ranga

Hello,
Can some one explain me the scope and life of a C struct. I saw a
piece of code that look like the following. My lint tool rejects the
code, but gcc accepts the code. I tried to play around with number and
type of the structure member list, still gcc accepts.

/* file f1.c */

int func_f1(void)
{
struct my_struct {
int a;
char *name;
int b;
} *list;

.... then some code after this that uses list
} /* end of func_f1() */

int func_f2(void)
{
struct my_struct {
int a;
char *name;
} *list;
..... then some code after this that uses list

} /* end of func_f2() */

As I know the tag of tagged structure will be unique. But gcc-3.3 (for
cygwin) allows this and from the the behaviour of the code I observe,
the scope limited to function.
Can some one explain this. Does K&R say any thing on this.

Or is this a new C standard ?

thanks
Ranga
 
M

Mark Odell

Ranga said:
Hello,
Can some one explain me the scope and life of a C struct. I saw a
piece of code that look like the following. My lint tool rejects the
code, but gcc accepts the code. I tried to play around with number and
type of the structure member list, still gcc accepts.

/* file f1.c */

int func_f1(void)
{
struct my_struct {
int a;
char *name;
int b;
} *list;

.... then some code after this that uses list
} /* end of func_f1() */

int func_f2(void)
{
struct my_struct {
int a;
char *name;
} *list;
..... then some code after this that uses list

} /* end of func_f2() */

As I know the tag of tagged structure will be unique. But gcc-3.3 (for
cygwin) allows this and from the the behaviour of the code I observe,
the scope limited to function.
Can some one explain this. Does K&R say any thing on this.

Or is this a new C standard ?

Not new.

void foo(void)
{
int var;
}

void bar(void)
{
int var;
}

Both vars are in differnent scopes so they are not the same, why should
struct tags, enums, typedefs etc. be any different. This is part of C.
 
C

Christian Kandeler

Ranga said:
Hello,
Can some one explain me the scope and life of a C struct. I saw a
piece of code that look like the following. My lint tool rejects the
code, but gcc accepts the code. I tried to play around with number and
type of the structure member list, still gcc accepts.

/* file f1.c */

int func_f1(void)
{
struct my_struct {
int a;
char *name;
int b;
} *list;

.... then some code after this that uses list
} /* end of func_f1() */

int func_f2(void)
{
struct my_struct {
int a;
char *name;
} *list;
..... then some code after this that uses list

} /* end of func_f2() */

Are you sure you're understanding the lint message right? What does it say
exactly? According to the standard (6.2.1 if the numbering is the same as
in the final draft), struct tags have the same scope as other identifiers,
so the code should be correct.


Christian
 
M

Michael Wojcik

Ranga said:
Can some one explain me the scope and life of a C struct.
piece of code that look like the following. My lint tool rejects the
code, but gcc accepts the code.
[snip example: struct my_struct defined differently in two scopes]

Are you sure you're understanding the lint message right? What does it say
exactly?

Note that lint-type tools often produce diagnostics for correct
constructs on the grounds that they may be "dangerous" in some form
(not portable, or potentially confusing for maintainers, or likely to
be coding errors rather than deliberate).

I suspect that's the case here.

(Of course compilers, too, are free to produce whatever diagnostics
they like. The appearance of a diagnostic is no guarantee that a
construct is invalid.)
According to the standard (6.2.1 if the numbering is the same as
in the final draft),

Or in C90, 6.5.2.3.
struct tags have the same scope as other identifiers,

That is, they have the same scoping rules as other identifiers,
except for the additional rule regarding completing an incomplete
tagged type (which must happen in the same scope as the incomplete
declaration, and not in an enclosed block). Since non-tagged types
can't be completed in this way, that rule doesn't apply to them.
 
C

CBFalconer

Ranga said:
Can some one explain me the scope and life of a C struct. I saw a
piece of code that look like the following. My lint tool rejects
the code, but gcc accepts the code. I tried to play around with
number and type of the structure member list, still gcc accepts.

....snip code etc...

I played around with your code slightly to make it compilable, and
the result generates no warnings in splint or gcc.

[1] c:\c\junk>splint junk.c
Splint 3.0.1.6 --- 11 Feb 2002

Finished checking --- no warnings

[1] c:\c\junk>cat junk.c
#include <stdlib.h>

static int func_f1(void)
{
struct my_struct {
int a;
char *name;
int b;
} *list;

list = malloc(sizeof *list);
free(list);
return 0;
} /* end of func_f1() */

static int func_f2(void)
{
struct my_struct {
int a;
char *name;
} *list;

list = malloc(sizeof *list);
free(list);
return 0;
} /* end of func_f2() */

int main(void)
{
int v;

v = func_f1();
v = func_f2();
return 0;
}
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top