correct macro definitions

S

sinbad

hi, i have a program like this. at line 18 i want z() to be called
and at line 19 i want y() to be called. The following macro defn's
doesn't seem to work (they replace both y() and x() with z() and z
()),
what's the correct way for doing this.

1 #include <stdlib.h>
2
3 #define x() y()
4 #define y() z()
5
6 void y() {
7 int i = 0;
8 i++;
9 }
10
11 void z(){
12 int j = 0;
13 j++;
14 }
15
16 int main ()
17 {
18 y(); /* i want z() to be called here */
19 x(); /* i want y() to be called here */
20 return 0;
21 }
 
N

Nate Eldredge

sinbad said:
hi, i have a program like this. at line 18 i want z() to be called
and at line 19 i want y() to be called. The following macro defn's
doesn't seem to work (they replace both y() and x() with z() and z
()),
what's the correct way for doing this.

1 #include <stdlib.h>
2
3 #define x() y()
4 #define y() z()
5
6 void y() {
7 int i = 0;
8 i++;
9 }
10
11 void z(){
12 int j = 0;
13 j++;
14 }
15
16 int main ()
17 {
18 y(); /* i want z() to be called here */
19 x(); /* i want y() to be called here */
20 return 0;
21 }

You could do

#define x() (y)()
#define y() z()

Also, you probably want to put them after the definitions of the
functions y() and z(), otherwise the functions are not going to be
defined the way you think.

Generally, writing (foo)() instead of foo() is a useful trick if you
want to be sure that foo is interpreted as a function instead of a
function-like macro.
 
M

Martin Ambuhl

sinbad said:
hi, i have a program like this. at line 18 i want z() to be called
and at line 19 i want y() to be called. The following macro defn's
doesn't seem to work (they replace both y() and x() with z() and z
()),
what's the correct way for doing this.

The short answer is you don't. There is no excuse for using the macro
processor as a way to make your code unreadable. Someone reading your
code should not be subjected to games being played so that the function
names he sees in the code, which names correspond exactly with function
names in scope, are lies. If you want to change those names, use your
text editor.

And, as a posting note, prefixing your code lines with line numbers is
just throwing an obstacle in the way of anyone who would like to help
you. Post code in a form that can be compiled.

If your goal is to gain some flexibility over what functions are called
in the code, rather than playing childish games with anyone trying to
read your code, consider the options below:


#include <stdio.h>
void y()
{
printf("In y()\n");
}

void z()
{
printf("In z()\n");
}

int main(void)
{
void (*f) ();
void (*fv[]) () = {z, y};

printf
("Calling functions using assignment to a function pointer\n");
f = z;
f();
f = y;
f();
printf("\nCalling functions using an array of function pointers\n");
fv[0] ();
fv[1] ();
return 0;
}

[output]

Calling functions using assignment to a function pointer
In z()
In y()

Calling functions using an array of function pointers
In z()
In y()
 
C

Chris M. Thomasson

sinbad said:
hi, i have a program like this. at line 18 i want z() to be called
and at line 19 i want y() to be called. The following macro defn's
doesn't seem to work (they replace both y() and x() with z() and z
()),
what's the correct way for doing this.
[...]

Try something like this:
_________________________________________________
#include <stdio.h>


void y() {
puts("void y();");
}


void z() {
puts("void z();");
}


#define x (y)
#define y() z()


int main(void) {
y();
x();
return 0;
}
_________________________________________________
 
M

Martin Ambuhl

Ben said:
And I am similarly at a loss as to yours. Why don't you offer help?

The tree suggests that I am the one the troll was critical of.
I am at a loss why people who know that they are killfiled and who know
I only see their posts if quoted keep directing barbs at me as if I read
their trash. The frequent quotation by others of such things from Han
from China, Kenny McCormack, and rgrdev, AKA Richard without any last
name, suggests that all of them hallucinate that I read their posts.
But it is not unexpected that trolls are also idiots.
 
K

Kenny McCormack

The tree suggests that I am the one the troll was critical of.
I am at a loss why people who know that they are killfiled and who
know I only see their posts if quoted keep directing barbs at me as if
I read their trash. The frequent quotation by others of such things
from Han from China, Kenny McCormack, and rgrdev, AKA Richard without
any last name, suggests that all of them hallucinate that I read their
posts. But it is not unexpected that trolls are also idiots.

Try reading the comments then you pompous arse. Your reply gave nothing.[/QUOTE]

The lady (Ambuhl) doth protest (way, way) too much.
 
W

wu

hi, i have a program like this. at line 18 i want z() to be called
and at line 19 i want y() to be called. The following macro defn's
doesn't seem to work (they replace both y() and x() with z() and z
()),
what's the correct way for doing this.

1 #include<stdlib.h>
2
3 #define x() y()
4 #define y() z()
5
6 void y() {
7 int i = 0;
8 i++;
9 }
10
11 void z(){
12 int j = 0;
13 j++;
14 }
15
16 int main ()
17 {
18 y(); /* i want z() to be called here */
19 x(); /* i want y() to be called here */
20 return 0;
21 }

A little modification will accomplish what you want.
1 #include <stdlib.h>
2
3 #define x() a()
4 #define y() b()
5
6 void a() {
7 int i = 0;
8 i++;
9 }
10
11 void b(){
12 int j = 0;
13 j++;
14 }
The problem lies in "y" in your definition. It is both a function name
and a macro name. And the two functions are not so good, they confuse me.
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top