CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    A strange usage of # and sizeof.

    Hello everyone,


    I have found a very strange usage of # and sizeof from the following statement,

    Code:
    #define FOO(p) static const struct_goo p = { .length = sizeof(#p "\n")};
    What means "#p"? What is the meaning of the parameter -- #p "\n" -- passed to sizeof?


    thanks in advance,
    George
    Last edited by George2; October 18th, 2005 at 09:21 PM.

  2. #2
    Join Date
    May 2005
    Location
    United States
    Posts
    526

    Re: A strange usage of # and sizeof.

    # is the stringizing operator, used to convert macro arguments into string constants. Details on its use can be found here. Here's a simpler example:
    Code:
    #include <iostream>
    using namespace std;
    
    #define FOO(p) int p = sizeof(#p "\n");
    
    int main()
    {
        FOO(murder);  // int murder = sizeof("murder" "\n");
        FOO(death);   // int death = sizeof("death" "\n");
        FOO(kill);    // int kill = sizeof("kill" "\n");
    
        cout << murder << '\n'   // OUTPUT: 8
             << death  << '\n'   //         7
             << kill   << '\n';  //         6
    
        return 0;
    }
    Here you can see an example of the use of the stringizing operator #. When you apply sizeof() to a string constant, it returns the size of the string in bytes. And when you have two strings one after the other, like "murder" "\n", they are concatenated into "murder\n". In the program above, the variable murder has a value of eight because there are six bytes used to store the letters, one to store the newline, and one for the terminating null.

    The one thing I don't understand about the code you posted is the syntax it uses for (apparently) attempting to initialize a struct. I've never seen that before, and VC++ 6.0 certainly doesn't like it. Can someone explain?

  3. #3
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: A strange usage of # and sizeof.

    Thanks Smasher/Devourer,


    Quote Originally Posted by Smasher/Devourer
    The one thing I don't understand about the code you posted is the syntax it uses for (apparently) attempting to initialize a struct. I've never seen that before, and VC++ 6.0 certainly doesn't like it. Can someone explain?
    I am not quite sure what points do not make you understand. Actually, I am using C other than C++. Could you comment the points you do not understand in my sample?


    regards,
    George

  4. #4
    Join Date
    Jul 2005
    Posts
    767

    Re: A strange usage of # and sizeof.

    Yes Thanks Smasher/Devourer,

    Here is an unified example of the two operators I did not know about

    Code:
    #define makechar(c) #@c
    #define makestring(p) #p
    
    int main()
    {
    	char c = makechar(b);
    	char* p = makestring(tyu);
    
    	return 0;
    }
    Last edited by MrBeans; October 19th, 2005 at 12:59 AM.

  5. #5
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: A strange usage of # and sizeof.

    Quote Originally Posted by George2
    Code:
    ... p = { .length = sizeof(#p "\n")};
    This kind of initializing of a structure won't work with the GCC compiler either. Which compiler are you using?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  6. #6
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: A strange usage of # and sizeof.

    Quote Originally Posted by George2
    Code:
    ... p = { .length = sizeof(#p "\n")};
    I know that Comeau allow that type of initialization : it is a "named" intialization, and allow to initialize a particular field of a structure, specifying its name.

    The exact name of this feature is : "designated initializers"
    Look at http://www.comeaucomputing.com/features.html
    Last edited by SuperKoko; October 19th, 2005 at 05:53 AM.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  7. #7
    Join Date
    May 2005
    Location
    United States
    Posts
    526

    Re: A strange usage of # and sizeof.

    Thanks SuperKoko, I'd never seen that before.

  8. #8
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: A strange usage of # and sizeof.

    I believe that designated initializers are in C99 , not C++

    EDIT:

    gcc version 3.4.3 supports it (when compiling C code, not C++ code)
    Last edited by Philip Nicoletti; October 19th, 2005 at 11:58 AM.

  9. #9
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: A strange usage of # and sizeof.

    Quote Originally Posted by Philip Nicoletti
    I believe that designated initializers are in C99 , not C++
    Yes, and Comeau does not allow them in C++, even in relaxed mode (i tried with the online compiler).
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  10. #10
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: A strange usage of # and sizeof.

    Thanks MrBeans,


    Quote Originally Posted by MrBeans
    Yes Thanks Smasher/Devourer,

    Here is an unified example of the two operators I did not know about

    Code:
    #define makechar(c) #@c
    #define makestring(p) #p
    
    int main()
    {
    	char c = makechar(b);
    	char* p = makestring(tyu);
    
    	return 0;
    }
    A very interesting sample!


    regards,
    George

  11. #11
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: A strange usage of # and sizeof.

    Marc,


    Quote Originally Posted by Marc G
    This kind of initializing of a structure won't work with the GCC compiler either. Which compiler are you using?
    I am not quite sure. Another guy is in charge of it. I just use some batch command customized by him to do development.


    regards,
    George

  12. #12
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: A strange usage of # and sizeof.

    Quote Originally Posted by George2
    Marc,

    I am not quite sure. Another guy is in charge of it. I just use some batch command customized by him to do development.

    regards,
    George
    Actually (according to Philip Nicoletti above) it appears to be possible with gcc 3.4.3 when compiling C and not C++ code.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  13. #13
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020

    Re: A strange usage of # and sizeof.

    #@ is Microsoft specific - it won't work on any other compiler. Anyway, it is a lot more to type. eg
    Code:
    #define T(x) #@x
    
    char  lesstotype = 'a';
    char moretotype = T(a);
    Even when defining a one character macro, you still type one more character than intended. I suppose it has its uses like converting to wide chars
    Code:
    #define U(x) L#@x
    wchar_t  unicode = U(J);
    Succinct is verbose for terse

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured