Converting #define value to a variable

M

maker.rain1

Hello All,

I have come across a problem as explained below in a sample. Please
help me if anyone has any ideas to solve this.

I have a #define as defined below.

#define MAX 200

int _tmain(int argc, _TCHAR* argv[])
{
int arr[MAX];
return 0;
}

Here we do not have any problem. I have a situation and I want to make
this MAX value as a variable. As per C rules we can not do that and
compiler gives error. In my actual problematic code this kind of code
is at lots of places. And also at certain places we have code like
below.

typedef struct _xxxxx
{
....
byte val;
uint array[MAX];
}xxxxx;

So what would be the best solution with minimal effort to replace
#define value MAX with a variable which I can change anytime.

Thanks in advance and awaiting for some response soon.
 
R

rahul

Hello All,

I have come across a problem as explained below in a sample. Please
help me if anyone has any ideas to solve this.

I have a #define as defined below.

#define MAX 200

int _tmain(int argc, _TCHAR* argv[])
{
int arr[MAX];
return 0;

}

Here we do not have any problem. I have a situation and I want to make
this MAX value as a variable. As per C rules we can not do that and
compiler gives error.

What's with _tmain and _TCHAR*? It's not standard C.
C99 allows variable length arrays. If you have a C99 complying
compiler, that can
do the trick for you.
typedef struct _xxxxx
{
....
byte val;
uint array[MAX];
}xxxxx;
So what would be the best solution with minimal effort to replace
#define value MAX with a variable which I can change anytime.

since when we have a byte data type in C?
You can declare the array as a pointer and allocate it memory based on
your variable in your code.

typedef struct _foo {
short num;
unsigned int* array;
}foo;
struct foo bar;

/* Later in your code */
bar.array = malloc (num * sizeof *bar.array);
 
B

Ben Bacarisse

I have a #define as defined below.

#define MAX 200

int _tmain(int argc, _TCHAR* argv[])
{
int arr[MAX];
return 0;
}
typedef struct _xxxxx
{
....
byte val;
uint array[MAX];
}xxxxx;

So what would be the best solution with minimal effort to replace
#define value MAX with a variable which I can change anytime.

There is no one solution to both problems. If you can use a C99
compiler (or an almost C99 compiler like gcc) you can get round both
of these but in different ways.

The first "just works". For the second, you leave out the size. When
the struct is allocated you must add in some extra space for the final
array. This means you can't usually declare variables of such a
struct type -- you must be using malloc.

If you don't have C99, the first problem has to be solved with:

int *arr = malloc(MAX * sizeof *arr);

and now MAX need not be a constant expression. Remember to free the
space.

For the struct problem you have two choices. The most portable is to
change the array to a pointer. Allocating one of these structs then
involves two mallocs -- one for the struct and one for the array.

If you don't mind sailing close to the rocks, the alternative is to
replace the array size by 1 and allocate the extra space (minus one
array element) as if you were using the C99 solution. This is not
guaranteed to work by the C standard, but it work on a very wide range
of systems. For reference, it is called "the struct hack".
 
S

Serve Lau

CBFalconer said:
I have come across a problem as explained below in a sample.
Please help me if anyone has any ideas to solve this.

I have a #define as defined below.

#define MAX 200
int _tmain(int argc, _TCHAR* argv[]) {
int arr[MAX];
return 0;
}

Here we do not have any problem. ...

Yes you do. The identifiers _tmain and _TCHAR are reserved for the
implementation. You are not allowed to use them. Avoid all
leading underscores.

they are names supplied by the implementation he just uses them. So nope, in
that sense there's no problem here
 
B

Barry Schwarz

Hello All,

I have come across a problem as explained below in a sample. Please
help me if anyone has any ideas to solve this.

I have a #define as defined below.

#define MAX 200

int _tmain(int argc, _TCHAR* argv[])
{
int arr[MAX];
return 0;
}

Here we do not have any problem. I have a situation and I want to make
this MAX value as a variable. As per C rules we can not do that and
compiler gives error. In my actual problematic code this kind of code
is at lots of places. And also at certain places we have code like
below.

typedef struct _xxxxx
{
....
byte val;
uint array[MAX];
}xxxxx;

So what would be the best solution with minimal effort to replace
#define value MAX with a variable which I can change anytime.
If you don't know the size of the array at compile time but you will
know at run time, replace the array with a pointer and use malloc to
allocate the space. Your code can reference elements of the "dynamic
array" with the same syntax as if you had defined the array.


Remove del for email
 

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,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top