Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Structure members and reserved identifiers

Reply
Thread Tools

Structure members and reserved identifiers

 
 
Bas Wassink
Guest
Posts: n/a
 
      06-26-2007
Hello all,

I've been wondering about struct member names and reserved identifiers
for some time now and I can't figure out whether the reserved identifier
restrictions apply to struct members.

I think the following is allowed:

struct foo {
unsigned char *memory;
};

Since struct members are in a different namespace from (than?) function
names, the member name 'memory' would not invade the implementations
namespace 'mem[a-z]*'.

Until now I've refrained from using member names such as 'string' and
'memory', just to be on the safe side, but sometimes such names are the
most descriptive for the data they represent.

I've been reading through the standard (n1124 draft) and my books on C
(K&R2 and 'Expert C Programming' by Peter van der Linden) but I can't
find a satisfactory answer.


So if anyone could shed some light on this issue, I would be much obliged,

Bas Wassink
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      06-26-2007
Bas Wassink said:

> Hello all,
>
> I've been wondering about struct member names and reserved identifiers
> for some time now and I can't figure out whether the reserved
> identifier restrictions apply to struct members.


They don't (although I'd still steer clear of keywords if I were you!).

The crux is that the kind of names you're (laudably) worrying about,
str*, mem*, to*, is*, and so on, are reserved for use as ***external
identifiers***. Each struct carries its own name space around, so
you're okay with a struct member called 'memory' (or indeed 'member').
You can also have struct members called 'towel', 'isobar', and
'strange_attractor' if you like.

The relevant text in C89 begins with:

4.13 FUTURE LIBRARY DIRECTIONS

The following names are grouped under individual headers for
convenience. All external names described below are reserved no
matter what headers are included by the program.

The relevant text in C99 begins with:

7.26 Future library directions

1 The following names are grouped under individual headers for
convenience. All external names described below are reserved no matter
what headers are included by the program.



--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
 
 
 
Bas Wassink
Guest
Posts: n/a
 
      06-26-2007
On Tue, 26 Jun 2007 08:43:10 +0000, Richard Heathfield wrote:

> Bas Wassink said:
>
>> Hello all,
>>
>> I've been wondering about struct member names and reserved identifiers
>> for some time now and I can't figure out whether the reserved
>> identifier restrictions apply to struct members.

>
> They don't (although I'd still steer clear of keywords if I were you!).


I certainly won't use those as member names, that would only cause
confusion.

> The crux is that the kind of names you're (laudably) worrying about,
> str*, mem*, to*, is*, and so on, are reserved for use as ***external
> identifiers***. Each struct carries its own name space around, so you're
> okay with a struct member called 'memory' (or indeed 'member'). You can
> also have struct members called 'towel', 'isobar', and
> 'strange_attractor' if you like.
>
> The relevant text in C89 begins with:
>
> 4.13 FUTURE LIBRARY DIRECTIONS
>
> The following names are grouped under individual headers for
> convenience. All external names described below are reserved no matter
> what headers are included by the program.
>
> The relevant text in C99 begins with:
>
> 7.26 Future library directions
>
> 1 The following names are grouped under individual headers for
> convenience. All external names described below are reserved no matter
> what headers are included by the program.


Right, the word *external* in those sections lead me to believe I could
indeed use things like 'string' and 'token' as struct member names, but I
wanted to be sure, that's why I thought I'd ask the experts.

Thank you very much,

Bas Wassink
 
Reply With Quote
 
Dave Hansen
Guest
Posts: n/a
 
      06-26-2007
On Jun 26, 3:43 am, Richard Heathfield <r...@see.sig.invalid> wrote:
> Bas Wassink said:
>
> > Hello all,

>
> > I've been wondering about struct member names and reserved identifiers
> > for some time now and I can't figure out whether the reserved
> > identifier restrictions apply to struct members.

>
> They don't (although I'd still steer clear of keywords if I were you!).
>
> The crux is that the kind of names you're (laudably) worrying about,
> str*, mem*, to*, is*, and so on, are reserved for use as ***external
> identifiers***. Each struct carries its own name space around, so
> you're okay with a struct member called 'memory' (or indeed 'member').
> You can also have struct members called 'towel', 'isobar', and
> 'strange_attractor' if you like.
>


But beware of reserved macro names if the associated header is
included. Macros don't respect namespaces.

Regards,

-=Dave


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-26-2007
Dave Hansen <> writes:
> On Jun 26, 3:43 am, Richard Heathfield <r...@see.sig.invalid> wrote:
>> Bas Wassink said:
>> > I've been wondering about struct member names and reserved identifiers
>> > for some time now and I can't figure out whether the reserved
>> > identifier restrictions apply to struct members.

>>
>> They don't (although I'd still steer clear of keywords if I were you!).
>>
>> The crux is that the kind of names you're (laudably) worrying about,
>> str*, mem*, to*, is*, and so on, are reserved for use as ***external
>> identifiers***. Each struct carries its own name space around, so
>> you're okay with a struct member called 'memory' (or indeed 'member').
>> You can also have struct members called 'towel', 'isobar', and
>> 'strange_attractor' if you like.

>
> But beware of reserved macro names if the associated header is
> included. Macros don't respect namespaces.


Right, but if mem* and friends are defined as macros, they're defined
as function-like macros.

But you can still run into problems if you use one of those identifers
as a member name, if the member is a function pointer. For example:

#include <string.h>
/* Assume the implementation defines a function memfoo(), and
additionally defines an equivalent function-like macro. */

struct mystruct {
void (*memfoo)(void);
}
struct mystruct obj;
/* ... */
obj.memfoo(); /* This invokes the macro! */

If you *don't* include <string.h>, either directly or indirectly,
there's no conflict with the external function.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Bas Wassink
Guest
Posts: n/a
 
      06-26-2007
On Tue, 26 Jun 2007 13:54:54 -0700, Keith Thompson wrote:

> Dave Hansen <> writes:
>> On Jun 26, 3:43 am, Richard Heathfield <r...@see.sig.invalid> wrote:
>>> Bas Wassink said:
>>> > I've been wondering about struct member names and reserved
>>> > identifiers for some time now and I can't figure out whether the
>>> > reserved identifier restrictions apply to struct members.
>>>
>>> They don't (although I'd still steer clear of keywords if I were
>>> you!).
>>>
>>> The crux is that the kind of names you're (laudably) worrying about,
>>> str*, mem*, to*, is*, and so on, are reserved for use as ***external
>>> identifiers***. Each struct carries its own name space around, so
>>> you're okay with a struct member called 'memory' (or indeed 'member').
>>> You can also have struct members called 'towel', 'isobar', and
>>> 'strange_attractor' if you like.

>>
>> But beware of reserved macro names if the associated header is
>> included. Macros don't respect namespaces.

>
> Right, but if mem* and friends are defined as macros, they're defined as
> function-like macros.
>
> But you can still run into problems if you use one of those identifers
> as a member name, if the member is a function pointer. For example:
>
> #include <string.h>
> /* Assume the implementation defines a function memfoo(), and
> additionally defines an equivalent function-like macro. */
>
> struct mystruct {
> void (*memfoo)(void);
> }
> struct mystruct obj;
> /* ... */
> obj.memfoo(); /* This invokes the macro! */
>
> If you *don't* include <string.h>, either directly or indirectly,
> there's no conflict with the external function.


But if I interpret the standard correctly, I could #undef the memfoo()
macro and then use 'void (*memfoo)(void)' as a member of a struct,
avoiding the macro-substition and not invading the implementation's
namespace.

Not that I would do such a thing, that would be unnecessarily confusing
(and very bad style in my opinion). I'm just curious if I've interpreted
the standard (7.1.3 and 7.1.4) correctly.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-26-2007
Bas Wassink wrote:
> Keith Thompson wrote:
>

.... snip ...
>
>> If you *don't* include <string.h>, either directly or indirectly,
>> there's no conflict with the external function.

>
> But if I interpret the standard correctly, I could #undef the
> memfoo() macro and then use 'void (*memfoo)(void)' as a member of
> a struct, avoiding the macro-substition and not invading the
> implementation's namespace.


You MIGHT get away with it, but it is not gu guaranteed by the
standard. So don't.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net



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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-26-2007
Bas Wassink <> writes:
> On Tue, 26 Jun 2007 13:54:54 -0700, Keith Thompson wrote:

[...]
>> Right, but if mem* and friends are defined as macros, they're defined as
>> function-like macros.
>>
>> But you can still run into problems if you use one of those identifers
>> as a member name, if the member is a function pointer. For example:
>>
>> #include <string.h>
>> /* Assume the implementation defines a function memfoo(), and
>> additionally defines an equivalent function-like macro. */
>>
>> struct mystruct {
>> void (*memfoo)(void);
>> }
>> struct mystruct obj;
>> /* ... */
>> obj.memfoo(); /* This invokes the macro! */
>>
>> If you *don't* include <string.h>, either directly or indirectly,
>> there's no conflict with the external function.

>
> But if I interpret the standard correctly, I could #undef the memfoo()
> macro and then use 'void (*memfoo)(void)' as a member of a struct,
> avoiding the macro-substition and not invading the implementation's
> namespace.
>
> Not that I would do such a thing, that would be unnecessarily confusing
> (and very bad style in my opinion). I'm just curious if I've interpreted
> the standard (7.1.3 and 7.1.4) correctly.


Yes -- and yes, it would be ugly.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: reserved identifiers (slightly OT) Thomas Richter C Programming 5 11-07-2010 10:51 PM
reserved identifiers? Tim H C++ 8 03-23-2007 03:40 PM
Are Python's reserved words reserved in places they dont need to be? metaperl Python 32 09-15-2006 02:02 PM
RE: Are Python's reserved words reserved in places they dont needtobe? Delaney, Timothy (Tim) Python 10 09-14-2006 04:17 PM
Re: Are Python's reserved words reserved in places they dont needtobe? Steve Holden Python 0 09-13-2006 08:44 AM



Advertisments