Howto do the impossible with #define ?>?

K

Keith Thompson

Ancient_Hacker said:
Sorry folks,, I thought we were talking about the very common (if you
Google for "C multi-dimensional dynamic arrays") trick of malloc()ing
an array of pointers to each row, then mallocing an array of pointers
to each of those arrays. Works, but that's about all.

Glad to hear C99 is up to FORTRAN59 level of array handling.

Until recently, Google by default didn't provide context for
followups. They've fixed this bug. Please take advantage of it by
leaving enough context in your followup so it makes sense by itself.
We can't necessarily easily see or remember the previous article.

For those who haven't read it, section 6 of the comp.lang.c FAQ,
<http://www.c-faq.com/>, provides a lot of good information in this
area.
 
A

Ancient_Hacker

Eric Sosman wrote:


I think it's wonderful there are a few language-lawyers out there. We
need them to argue about the fine points of a language, such as how
many sequence points can fit on the head of a pin. Or why there's a
stringify escape character, but no char-ify escape. Many years ago
one of my friends was on a standards comittee, and he spent years
flying to monthly meetings where they endlessly discussed such things,
until finally the language in question just expired due to lack of a
standard.

All in the good old tradition, going back to Aristotle, where a bunch
of smart guys in togas would gather on a hilltop and grope each other
under the table while arguing eloquently and forecefully what the right
number of teeth were for a horse. One would argue that 24 was the
right number, as that's the number of vestal virgins on the Agean
corniche. Others would just as forcefully argue that 28 is the number,
as 2 plus eight is ten, the number of fingers Jove gave to Man.

But the other 99.5% of us are interested in more prosaic things, like
how to get our beleepin job done with this IMHO just about the most
cranky and train-wrecky of languages.

Now I'm not complaining TOO much, but when a guy asks for the answer to
how to remove the lug-nuts on a 66 Dodge Charger, he isnt helped much
by being given vague hints and insults as to how he's a moron cause he
doesnt know how the steel for the lug-wrench was forged. Much more
useful to gently remind him the lug-nuts on the right side are
left-hand thread.

When a guy asks how to put out the fire on his head, it doesnt help to
discourse on the quantum-principles that give H2O a high heat of
vaporization. Much better to suggest dunking his head in some nearby
lake. Which I sometimes need even when not on fire.

If you look back, there are 20+ replies to my question, not a single
one of them a clear or complete solution. A couple of good hints,
using which I was able to figure out that yes, for some unfathomable
reason, if you declare a 3-d array and index it with FOUR subscripts,
voila, the right indexing code gets generated. But also an awful lot
of self-preening comments and probably intentional ingenuousness.

I admit I participated in some misunderstanding as to the exact method
being suggested. I also should have explained exactly what I was
wanting to do, not asked for one dubious way of finishing up my
solution.

but still without clear concise and full and to the point examples,
confusion and digressions just blossom.

So not to overstate the obvious, when somebody asks a simple, basic
question, wouldnt it be nice to just reply with an answer, rather than
to stroke one's own ego by showing off how erudite one is in citing
chapter, verse, sentence, word, and syllable of something totally
unhelpful to the direct object of discourse?
 
C

Chris Torek

So not to overstate the obvious, when somebody asks a simple, basic
question, wouldnt it be nice to just reply with an answer ...

OK: the answer is, it is quite literally impossible to do what you
asked for.

You seem to hate the idea that someone might quote parts of the
standards to you, but here is the part that, in effect, says "you
cannot have a #define create a new #define":

[#1] After all parameters in the replacement list have been
substituted and # and ## processing has taken place, all
placemarker preprocessing tokens are removed, then the
resulting preprocessing token sequence is rescanned with all
subsequent preprocessing tokens of the source file for more
macro names to replace.

[#2] If the name of the macro being replaced is found during
this scan of the replacement list (not including the rest of
the source file's preprocessing tokens), it is not replaced.
Further, if any nested replacements encounter the name of
the macro being replaced, it is not replaced. These
nonreplaced macro name preprocessing tokens are no longer
available for further replacement even if they are later
(re)examined in contexts in which that macro name
preprocessing token would otherwise have been replaced.

[#3] The resulting completely macro-replaced preprocessing
token sequence is not processed as a preprocessing directive
*even if it resembles one* ... [emphasis mine]

That last clause means that no matter how you trick a C compiler
into taking:

#define THIS(x) \
<whatever replacement text you like, including a "#" and "define">

so that the result includes the sequence:

#define THAT(x) (*x)

this resulting "#define" will not be acted-upon.

You have to give up on this approach. There are others, though.
Re-read Eric Sosman's replies, and you will find a simpler method
that works just fine for what you *really* wanted to do. Or, if
you want to write unnecessary code (instead of using the simpler
method he suggested), write your own "pre-preprocessor" and run
your code through that before running it through a C compiler.
 
J

jaysome

Eric Sosman wrote:

Bravo. Excellent post.
I think it's wonderful there are a few language-lawyers out there. We
need them to argue about the fine points of a language, such as how
many sequence points can fit on the head of a pin. Or why there's a
stringify escape character, but no char-ify escape. Many years ago
one of my friends was on a standards comittee, and he spent years
flying to monthly meetings where they endlessly discussed such things,
until finally the language in question just expired due to lack of a
standard.

And you could say almost the same thing if your friend was on the C99
standards comittee:

"Many years ago one of my friends was on a standards comittee, and he
spent years flying to monthly meetings where they endlessly discussed
such things, until finally the language in question just expired us
due to lack of a standard agreement. And thus C99 was born."

[snip]
Now I'm not complaining TOO much, but when a guy asks for the answer to
how to remove the lug-nuts on a 66 Dodge Charger, he isnt helped much
by being given vague hints and insults as to how he's a moron cause he
doesnt know how the steel for the lug-wrench was forged.

Even someone who knows how to "steal the lug-wrench" knows how to
remove the lug-nuts on a 66 Dodge Charger. That's more progressive
than most morons. Sad but (NULL == 0) (the latter expression evaluates
to "true", for you morons :^)).
When a guy asks how to put out the fire on his head, it doesnt help to
discourse on the quantum-principles that give H2O a high heat of
vaporization. Much better to suggest dunking his head in some nearby
lake. Which I sometimes need even when not on fire.

Pointed pursuasiveness followed up by an expression of humbleness.
Brilliant.
If you look back, there are 20+ replies to my question, not a single
one of them a clear or complete solution. A couple of good hints,
using which I was able to figure out that yes, for some unfathomable
reason, if you declare a 3-d array and index it with FOUR subscripts,
voila, the right indexing code gets generated. But also an awful lot
of self-preening comments and probably intentional ingenuousness.

I admit I participated in some misunderstanding as to the exact method
being suggested. I also should have explained exactly what I was
wanting to do, not asked for one dubious way of finishing up my
solution.

but still without clear concise and full and to the point examples,
confusion and digressions just blossom.

So not to overstate the obvious, when somebody asks a simple, basic
question, wouldnt it be nice to just reply with an answer, rather than
to stroke one's own ego by showing off how erudite one is in citing
chapter, verse, sentence, word, and syllable of something totally
unhelpful to the direct object of discourse?

Yes.

All we need now for this thread to end is for Chris Torek to respond
to your post with a smack of reality that Every Good Boy who Does Fine
should read.

Take care
 
E

Eric Sosman

Richard Heathfield wrote On 07/07/06 11:35,:
Eric Sosman said:
Richard said:
Eric Sosman said:

Keep in mind that C really doesn't have multi-dimensional arrays:

I must take issue with you there. C /does/ have multi-dimensional arrays,
because the Standard says it does. 3.3.2.1 and 3.5.7 have plenty of
references to them. (In C99, see 6.5.2.1.)

Well, since the Standard uses the term I guess I have to
concede it. IMHO, though,

[Objections 1 through 4 - snipped]

Still, if the Standard says so ... All right, I yield.


Your objections are very sensible, but you are right to yield. Let us never
forget that sensible != normative. :)

Hah! I *knew* I wasn't just dreaming when I said C
didn't have multi-dimensional arrays. One authoritative
source uses the term "multi-dimensional array," but says

"In C, by definition a two-dimensional array is
really a one-dimensional array, each of whose
elements is an array."
-- Brian W. Kernighan and Dennis M. Ritchie,
"The C Programming Language," p. 104

However, even K&R, though authoritative, are non-normative.
 
A

Ancient_Hacker

Okay, so it's not doable. But here's two ways to do it:


cpp %Fn% | cc %CCOpts% # pre-process the file *twice*


perl -e "s/%ENV{'AList'}/\(\*\1\)/g" %Fn% | cc


.... and of course the adumbrated way of defining a 3D array and using 4
subscripts. Stimulates one's gag reflex the first 77 times you use it,
but hey, it's C.
 
J

Jalapeno

Ancient_Hacker said:
Okay, so it's not doable. But here's two ways to do it:


cpp %Fn% | cc %CCOpts% # pre-process the file *twice*


perl -e "s/%ENV{'AList'}/\(\*\1\)/g" %Fn% | cc


... and of course the adumbrated way of defining a 3D array and using 4
subscripts. Stimulates one's gag reflex the first 77 times you use it,
but hey, it's C.


READY


cpp %Fn% | cc %CCOpts% # pre-process the file *twice*


cpp: line 0, Error: Too many file arguments. Usage: cpp :input
:eek:utput::

READY


perl -e "s/%ENV{'AList'}/\(\*\1\)/g" %Fn% | cc


IKJ56500I COMMAND PERL NOT FOUND


READY



Doesn't work.
 
B

Ben Pfaff

Ancient_Hacker said:
Jeeepers, what OS is *that*? Univac 1100 OS? Chippewa? George IV?
Multics? :)

A quick Google says z/OS, aka MVS, aka OS/390, aka other things too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top