|
-
October 18th, 2005, 09:07 PM
#1
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.
-
October 18th, 2005, 10:28 PM
#2
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?
-
October 19th, 2005, 12:08 AM
#3
Re: A strange usage of # and sizeof.
Thanks Smasher/Devourer,
 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
-
October 19th, 2005, 12:56 AM
#4
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.
-
October 19th, 2005, 02:35 AM
#5
Re: A strange usage of # and sizeof.
 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?
-
October 19th, 2005, 05:46 AM
#6
Re: A strange usage of # and sizeof.
 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()!
-
October 19th, 2005, 10:45 AM
#7
Re: A strange usage of # and sizeof.
Thanks SuperKoko, I'd never seen that before.
-
October 19th, 2005, 11:39 AM
#8
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.
-
October 19th, 2005, 04:41 PM
#9
Re: A strange usage of # and sizeof.
 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()!
-
October 20th, 2005, 01:24 AM
#10
Re: A strange usage of # and sizeof.
Thanks MrBeans,
 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
-
October 20th, 2005, 01:27 AM
#11
Re: A strange usage of # and sizeof.
Marc,
 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
-
October 20th, 2005, 04:34 AM
#12
Re: A strange usage of # and sizeof.
 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.
-
October 20th, 2005, 04:48 AM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|