#def question

A

ashish

Hello,

Supposing I have a #def like so ..

#define A 10

Now A has been used everywhere in the code.

In loops ... for (i=0; i < A; i++)

And defines ...

int charArray[A];

And in structures

struct a {
int intarray[a];
};

Now, I want to define a variable B such that ..

#define B ( (my_mode) ? 20 : A)

Basically, B will be A (10 if my_mode is 0 (old) or 20 if my_mode is 1
(new).

Obviously this creates a problem with the integer array and the
structure definitions - I think I can do this only if it is a function
or in the for loop as above.

Is there any way I can achieve this or will I have to hardcode
#define B 20 and live with

int charArray [20]; etc. in all modes .

TIA
 
R

RCollins

ashish said:
Hello,

Supposing I have a #def like so ..

#define A 10

Now A has been used everywhere in the code.

In loops ... for (i=0; i < A; i++)

And defines ...

int charArray[A];

And in structures

struct a {
int intarray[a];
};

Now, I want to define a variable B such that ..

#define B ( (my_mode) ? 20 : A)

Basically, B will be A (10 if my_mode is 0 (old) or 20 if my_mode is 1
(new).

Obviously this creates a problem with the integer array and the
structure definitions - I think I can do this only if it is a function
or in the for loop as above.

Is there any way I can achieve this or will I have to hardcode
#define B 20 and live with

int charArray [20]; etc. in all modes .

TIA

If you know what "my_mode" will be at compile time, can you do
something like:

#define MY_MODE 1 /* or 0, depending on your needs */

#if MY_MODE
#define A 10
#else
#define A 20
#endif
 
B

bowsayge

ashish said to us:

[...]
Now, I want to define a variable B such that ..

#define B ( (my_mode) ? 20 : A)
[...]

As long as my_mode is a compile-time constant it will work. If not, and
if you're using GCC, you can use a non-standard GCC extension that allows
you to create automatic arrays that have variable sizes.
 
A

Arthur J. O'Dwyer

If you know what "my_mode" will be at compile time,

....then he could do exactly what he already said.
#define B ((my_mode)? 20: A)
The conclusion, then, is that he does /not/ know the value of 'my_mode'
at compile time.

C99's variable-length arrays alleviate some of the OP's problems,
but I think the proper response here is: "What are you /really/ trying
to do?"
<guess>The correct answer might involve 'malloc' and 'free'.</guess>

-Arthur
 
R

RCollins

Arthur said:
...then he could do exactly what he already said.
#define B ((my_mode)? 20: A)

Maybe ... assuming "my_mode" is *not* a macro constant, but rather
a variable set at runtime, is the following construct valid:

void my_func(void) {
int my_mode;

my_mode = some_func(some_args); /* to get a value for "my_mode" */

int my_array[my_mode ? 10 : 20]; / to create the array */

/* ... rest of function in here ... */

return; /* end of my_func */
}

Note that the definition of "my_array" just used an expansion
of macro "B". Is this legal? If so, then the OP's problem
is solved (except, maybe, for a bunch of typing and re-structuring
of his code).

If is _not_ legal, then he'll need to know the value of "my_mode"
at compile time; in which case the 'style' he uses to define
A and/or B is a matter of personal preference.
The conclusion, then, is that he does /not/ know the value of 'my_mode'
at compile time.

C99's variable-length arrays alleviate some of the OP's problems,
but I think the proper response here is: "What are you /really/ trying
to do?"
<guess>The correct answer might involve 'malloc' and 'free'.</guess>

I suspect you are right on both counts.
 
D

Dave Thompson

On 28 Aug 2004 00:41:16 GMT, RCollins <[email protected]>
wrote:
Maybe ... assuming "my_mode" is *not* a macro constant, but rather
a variable set at runtime, is the following construct valid:

void my_func(void) {
int my_mode;

my_mode = some_func(some_args); /* to get a value for "my_mode" */

int my_array[my_mode ? 10 : 20]; / to create the array */
Only in C99, or in gcc as an extension to C89. And a declaration after
a statement (in a block) is also legal only under the same conditions,
although that could "trivially" be fixed.

<snip>

- David.Thompson1 at worldnet.att.net
 

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,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top