By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,657 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

Preprocessor errors?

P: n/a
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?

--
Sensei <se******@mac.com>

Part of the inhumanity of the computer is that, once it is competently
programmed and working smoothly, it is completely honest. (Isaac Asimov)

Dec 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Sensei wrote:
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

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Dec 14 '05 #2

P: n/a
Sensei <se******@mac.com> wrote:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 14 '05 #3

P: n/a
On Wed, 14 Dec 2005 21:02:42 +0100, in comp.lang.c , Sensei
<se******@mac.com> wrote:
#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.
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.