Preprocessor errors?

S

Sensei

Hi!

I'm getting some weird results from a (probably) valid C expression
with some #define... I'm really puzzled.

A sample code is this (just the variables and defines, not actual code):

#define themacro(p, i) (p)[(i)]

int *ptr, *anotherptr, counter;

themacro(ptr, counter)=anotherptr=MEMORY_ALLOCATION(...);

Where memory_allocation is a function which mallocs and reallocs
memory. I get a weird null pointer somewhere, and just writing:

anotherptr=MEMORY_ALLOCATION(...);
themacro(ptr, counter)=anotherptr;

solves the problem. Am I doing something out of standard?
 
A

Artie Gold

Sensei said:
Hi!

I'm getting some weird results from a (probably) valid C expression with
some #define... I'm really puzzled.

A sample code is this (just the variables and defines, not actual code):

#define themacro(p, i) (p)[(i)]

int *ptr, *anotherptr, counter;

themacro(ptr, counter)=anotherptr=MEMORY_ALLOCATION(...);

Where memory_allocation is a function which mallocs and reallocs memory.
I get a weird null pointer somewhere, and just writing:

anotherptr=MEMORY_ALLOCATION(...);
themacro(ptr, counter)=anotherptr;

solves the problem. Am I doing something out of standard?
Show a compilable snippet that exhibits the problem. *Real code*. Cut
and paste.

HTH,
--ag
 
C

Christopher Benson-Manica

Sensei said:
themacro(ptr, counter)=anotherptr=MEMORY_ALLOCATION(...);
anotherptr=MEMORY_ALLOCATION(...);
themacro(ptr, counter)=anotherptr;
solves the problem. Am I doing something out of standard?

Possibly; I'm not much of a guru to tell you. If you haven't
already, however, I would investigate your platform's documentation
for how to invoke only the preprocessor on your code (gcc -E will do
it, for example) to make sure the preprocessed line looks the way you
expect it to.
 
M

Mark McIntyre

#define themacro(p, i) (p)[(i)]

by convention, user-defined macros are UPPERCASE so they can be
identified easier.
int *ptr, *anotherptr, counter;

themacro(ptr, counter)=anotherptr=MEMORY_ALLOCATION(...);

themacro() will create an object (*ptr)[(counter)] which has type int.
This isn't compatible with anotherptr which is of type int*.
Where memory_allocation is a function which mallocs and reallocs
memory. I get a weird null pointer somewhere, and just writing:

anotherptr=MEMORY_ALLOCATION(...);
themacro(ptr, counter)=anotherptr;
solves the problem.

this is just as bad - if you don't get a warning here, you should turn
up warninglevels in your compiler.
Am I doing something out of standard?

Remember how macro substitution works - its literal text replacement.
So you can write down what themacro() will do.
 

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
473,777
Messages
2,569,604
Members
45,211
Latest member
NelleWilde

Latest Threads

Top