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
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?
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
Re: A strange usage of # and sizeof.
Yes Thanks Smasher/Devourer, :thumb:
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;
}
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?
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
Re: A strange usage of # and sizeof.
Thanks SuperKoko, I'd never seen that before. :thumb:
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)
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).
Re: A strange usage of # and sizeof.
Thanks MrBeans,
Quote:
Originally Posted by MrBeans
Yes Thanks Smasher/Devourer, :thumb:
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
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
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.
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);