473,396 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Converting #define value to a variable

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.
Jun 27 '08 #1
5 3439
On Jun 6, 3:28 pm, maker.ra...@gmail.com wrote:
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);
Jun 27 '08 #2
ma*********@gmail.com writes:
I have a #define as defined below.

#define MAX 200

int _tmain(int argc, _TCHAR* argv[])
{
int arr[MAX];
return 0;
}
<snip>
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".

--
Ben.
Jun 27 '08 #3
ma*********@gmail.com wrote:
>
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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #4

"CBFalconer" <cb********@yahoo.comschreef in bericht
news:48***************@yahoo.com...
ma*********@gmail.com wrote:
>>
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

Jun 27 '08 #5
On Fri, 6 Jun 2008 03:28:35 -0700 (PDT), ma*********@gmail.com wrote:
>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
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
3
by: axial | last post by:
Is there a way to define a variable by resolving other variables? I'm trying to build an "uber-variable" from user-selected components. I have control over the XML and can change it as needed. ...
9
by: Coleen | last post by:
Hi All :-) I found the way to get my column sum (Thanks Cor I did it a little different, but the result is what I wanted) I used: dt_stat_report_3b.Columns.Add(New DataColumn("Sum",...
12
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
0
by: anide | last post by:
Hi all I’ve some problem, I’m trying to converting a sorting algorithm from C++ to C#. In C++ I’ve compiled it using MSVC and its working properly, and in C# I’m using .NET Framework 2.0 (Visual...
6
by: canoewhiteh2o | last post by:
I am converting a couple of C header files to C#. It is mainly just a bunch C structs but I am not sure how to handle the #ifdef and #ifndef in C#. For example: #ifndef DATE_TIME #define...
4
by: venkat | last post by:
I have come across some preprossor statements, such as #define PPTR_int #define PPTR_str #define DDAR_baddr & #define DDAR_caddr & What do they mean, but when i compile the code with these...
15
by: allthecoolkidshaveone | last post by:
I want to convert a string representation of a number ("1234") to an int, with overflow and underflow checking. Essentially, I'm looking for a strtol() that converts int instead of long. The...
12
by: cmdolcet69 | last post by:
Can anyone give me some ideas on how to convert the following program to vb 2003? I would like to jsut take this code and implement it in vb. def.h /* Global Type Definitions */ typedef ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.