[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  Programming
. -> [Folder]  General
. . -> [Subject]  Macros in C
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Macros in C

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Greven   Canada  (835 posts)  [Biography] bio
Date Tue 22 Jun 2004 11:44 PM (UTC)
Message
Anyone happen to know what header file the __line__ and __file__ macros are located in? Or more to the point, does anyone know what the __string macro does, and what file I would need to include to use it? Never heard about this one before, and I don't even know where to look via man pages.

Nobody ever expects the spanish inquisition!

darkwarriors.net:4848
http://darkwarriors.net
[Go to top] top

Posted by Nick Cash   USA  (626 posts)  [Biography] bio
Date Reply #1 on Tue 22 Jun 2004 11:52 PM (UTC)
Message
__LINE__ and __FILE__ are in stdio.h I believe. Never heard of the string one.

~Nick Cash
http://www.nick-cash.com
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #2 on Wed 23 Jun 2004 12:03 AM (UTC)
Message
__LINE__ and __FILE__ aren't defined in stdio.h. They're defined by the standard as being the current line and file being read. They are expanded by the preprocessor.

I haven't heard of __string I don't think, but I would guess that it takes its argument and puts "" around it.
e.g. __STRING__(x) --> "x"

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Nick Cash   USA  (626 posts)  [Biography] bio
Date Reply #3 on Wed 23 Jun 2004 12:07 AM (UTC)
Message
Whoops, guess lookin at examples of macro's isn't the way to go since you need stdio.h to do anything anyways. :P

~Nick Cash
http://www.nick-cash.com
[Go to top] top

Posted by Greven   Canada  (835 posts)  [Biography] bio
Date Reply #4 on Wed 23 Jun 2004 12:11 AM (UTC)
Message
Well, here is what its used in:
#define CHECK_LINKS(first, last, next, prev, type)		\
do {								\
  type *ptr, *pptr = NULL;					\
  if ( !(first) && !(last) )					\
    break;							\
  if ( !(first) )						\
  {								\
    bug( "CHECK_LINKS: last with NULL first!  %s.",		\
        __STRING(first) );					\
    for ( ptr = (last); ptr->prev; ptr = ptr->prev );		\
    (first) = ptr;						\
  }								\
  else if ( !(last) )						\
  {								\
    bug( "CHECK_LINKS: first with NULL last!  %s.",		\
        __STRING(first) );					\
    for ( ptr = (first); ptr->next; ptr = ptr->next );		\
    (last) = ptr;						\
  }								\
  if ( (first) )						\
  {								\
    for ( ptr = (first); ptr; ptr = ptr->next )			\
    {								\
      if ( ptr->prev != pptr )					\
      {								\
        bug( "CHECK_LINKS(%s): %p:->prev != %p.  Fixing.",	\
            __STRING(first), ptr, pptr );			\
        ptr->prev = pptr;					\
      }								\
      if ( ptr->prev && ptr->prev->next != ptr )		\
      {								\
        bug( "CHECK_LINKS(%s): %p:->prev->next != %p.  Fixing.",\
            __STRING(first), ptr, ptr );			\
        ptr->prev->next = ptr;					\
      }								\
      pptr = ptr;						\
    }								\
    pptr = NULL;						\
  }								\
  if ( (last) )							\
  {								\
    for ( ptr = (last); ptr; ptr = ptr->prev )			\
    {								\
      if ( ptr->next != pptr )					\
      {								\
        bug( "CHECK_LINKS (%s): %p:->next != %p.  Fixing.",	\
            __STRING(first), ptr, pptr );			\
        ptr->next = pptr;					\
      }								\
      if ( ptr->next && ptr->next->prev != ptr )		\
      {								\
        bug( "CHECK_LINKS(%s): %p:->next->prev != %p.  Fixing.",\
            __STRING(first), ptr, ptr );			\
        ptr->next->prev = ptr;					\
      }								\
      pptr = ptr;						\
    }								\
  }								\
} while(0)


Used in this context, I assume that it casts a pointer of unknown type to a string, but I dunno for sure.

Nobody ever expects the spanish inquisition!

darkwarriors.net:4848
http://darkwarriors.net
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #5 on Wed 23 Jun 2004 01:02 AM (UTC)
Message
No, I don't think so. first is actually a macro variable and is probably some variable... e.g. ch->first_carrying.

When you do checklinks(ch->first_carrying,...), first expands to ch->first_carrying. Then, when you do __STRING() on it, you get "ch->first_carrying". This way, you get to output the variable name you're checking, as opposed to just the pointer address (which isn't very helpful.)

If it were casting a pointer to a string, how could that be useful? You'd get a string of gibberish up until the first null byte. That'd be a kind of silly function. :)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Greven   Canada  (835 posts)  [Biography] bio
Date Reply #6 on Wed 23 Jun 2004 01:06 AM (UTC)
Message
While I agree that you would not be getting good information out of it, I also agree that "ch->first_carrying" wouldn't exactly be that handy either, so I'm totally stumped.

Nobody ever expects the spanish inquisition!

darkwarriors.net:4848
http://darkwarriors.net
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #7 on Wed 23 Jun 2004 01:14 AM (UTC)
Message
Sure it would be handy. You'd know that the character carrying list is screwing up as opposed to e.g. the room's list of people or contents. If you'd like I can whip up a small test script but I'm nearly positive that the string macro just puts on the quotation marks.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Greven   Canada  (835 posts)  [Biography] bio
Date Reply #8 on Wed 23 Jun 2004 02:04 AM (UTC)
Message
K, so I guess that its probably defined something like:

#define __STRING(x) "(x)"

Reason I'm asking is that it seems to be undefined in cygwin, as I making sure that it complies nicely on a bunch of different platforms. freebsd is next I guess, then RH.

Nobody ever expects the spanish inquisition!

darkwarriors.net:4848
http://darkwarriors.net
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #9 on Wed 23 Jun 2004 02:50 AM (UTC)
Message
Yup.

int main()
{
    cout << __STRING(hello) << endl;

    return 0;
}


On RH, this prints out:
hello

As I would have expected.

I confirm that on Cygwin it does not seem to work.

Your define won't work; you need to do:

#define __STRING(x) #x

I believe that the # operator is the "correct" way of doing __STRING.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


4,752 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]