473,326 Members | 2,099 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,326 software developers and data experts.

Trying to convert a macro from C++ to C# What does this do?

I'm trying to convert this macro to a c# function but I have a big problem.
It's on the LEFT side of an assignment statement and I am extremely
flustered over this one because I'm a little rusty and have been struggling
over this for days, not wanting to show all my ignorance in final desparate
plea for help which this is ...

Here is the macro ...

#define X(t,b) (sp->x[(t)*sp->beecount+(b)])

sp is a pointer to a structure like this

typedef struct {
int beecount; // beecount is set to 144
int *x // is set to a random pixel X location.
} swarmstruct

x is allocated memory thusly ...

sp->x = (short *) malloc(sizeof(short) * sp->beecount * TIMES); // TIMES is
a constant = 4

Later, X is called like this ..
for (b = 0; b < sp->beecount; b++) {
X(0,b) = rand() % width; // width is the screen width.
X(1, b) = X(0, b);
}

What confuses me is that when I try to translate this I get the following
for the first call ...

sp->x[(0)*sp->beecount+(0)]

I am confused by this. If I take this * operator as meaning the indirection
operator than what does the (0) I get what appears to make no sense to me
.... x[(0)MemLoc(beecount)+0] = a random pixal x location and am trying to
assign a number to an address and treating an address as an indexor?

If I take the * to mean multiply than I am doing x[0 * sp->beecount + b] =
random pixal x location but that is so redundant because it just comes down
to x[b] = something.

I am equally confused by the second call which to me looks like it should
create an overflow error.

I do not understand what is happening here. There is no real meaningful
documentation and the author allows the inclusion of the source code as long
as I mention his name in the ending program.

I would have put this in the C++ forum here but there ISN'T one, and I'm
trying to convert it to c# using arrays instead of pointers and I'm stuck
bigtime on this point.

Anyone have any hints as to how I should convert or interprety this macro?
I've been trying to interpret it by simply doing the pre-processing inlining
by hand since c# has no macro ability but since the macros use pointers and
I'm trying to convert to array logic, I haven't a hope of interpreting this
until I understand what X is supposed to do.

Thanks for any help. /hangs my head in shame of his ignorance.

Nov 17 '05 #1
4 1884
I can't seem to get any replies via email. I get the following. This is a
test to see if my modified email address shows up. I wish I knew how to stop
getting this bouncing as below.

Unknown host: Ga******@community.nospam
Nov 17 '05 #2
If I understand this correctly, your second understand would be the correct
interpretation.

#define X(t, b) (sp->x[(t) * sp->beecount + (b)])

I don't think the "sp" is a pointer to a pointer! So, you should be looking
at the product of "t" and "sp->beecount", later adding "b".

James

"Garry Freemyer" <Ga******@community.nospam> wrote in message
news:eY**************@TK2MSFTNGP15.phx.gbl...
I'm trying to convert this macro to a c# function but I have a big
problem. It's on the LEFT side of an assignment statement and I am
extremely flustered over this one because I'm a little rusty and have been
struggling over this for days, not wanting to show all my ignorance in
final desparate plea for help which this is ...

Here is the macro ...

#define X(t,b) (sp->x[(t)*sp->beecount+(b)])

sp is a pointer to a structure like this

typedef struct {
int beecount; // beecount is set to 144
int *x // is set to a random pixel X location.
} swarmstruct

x is allocated memory thusly ...

sp->x = (short *) malloc(sizeof(short) * sp->beecount * TIMES); // TIMES
is a constant = 4

Later, X is called like this ..
for (b = 0; b < sp->beecount; b++) {
X(0,b) = rand() % width; // width is the screen width.
X(1, b) = X(0, b);
}

What confuses me is that when I try to translate this I get the following
for the first call ...

sp->x[(0)*sp->beecount+(0)]

I am confused by this. If I take this * operator as meaning the
indirection operator than what does the (0) I get what appears to make no
sense to me ... x[(0)MemLoc(beecount)+0] = a random pixal x location and
am trying to assign a number to an address and treating an address as an
indexor?

If I take the * to mean multiply than I am doing x[0 * sp->beecount + b] =
random pixal x location but that is so redundant because it just comes
down to x[b] = something.

I am equally confused by the second call which to me looks like it should
create an overflow error.

I do not understand what is happening here. There is no real meaningful
documentation and the author allows the inclusion of the source code as
long as I mention his name in the ending program.

I would have put this in the C++ forum here but there ISN'T one, and I'm
trying to convert it to c# using arrays instead of pointers and I'm stuck
bigtime on this point.

Anyone have any hints as to how I should convert or interprety this macro?
I've been trying to interpret it by simply doing the pre-processing
inlining by hand since c# has no macro ability but since the macros use
pointers and I'm trying to convert to array logic, I haven't a hope of
interpreting this until I understand what X is supposed to do.

Thanks for any help. /hangs my head in shame of his ignorance.

Nov 17 '05 #3
The macro is attempting to simulate a 2 dimentional array: X(a,b)
roughly equals x[a][b]. However, that would require at least one dimension
being fixed. Of course, in this design, one dimension *is* fixed, but I
guess at some point in the design phase TIMES was a variable. In other
words, the C code (and this is C code, not C++) could have been written as:

// type INT_TIMES is an array of 4 integers.
typedef int INT_TIMES[TIMES];
typedef struct {
int beecount:
INT_TIMES* x; // x points to an array of INT_TIMES objects.
}swarmstruct;

sp->beecount = 144;
sp->x = malloc(sp->beecount * sizeof(INT_TIMES));

for (b = 0; b < sp->beecount; b++)
{
// note I had to reverse the indexes here.
sp->x[b][0] = rand() % width;
sp->x[b][1] = sp->x[b][0];
}
Now that we (hopefully) understand what's going on, it should be easy to
translate that into C#:

const int TIMES = 4;
struct swarmstruct
{
int beecount;
int[,] x;
}

sp.beecount = 144;
sp.x = new int[TIMES, sp.beecount]
for (b = 0; b < sp.beecount; b++)
{
// Note, the indexes are back in their original order.
sp.x[0][b] = rand() % width;
sp.x[1][b] = sp->x[0][b];
}

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Garry Freemyer" <Ga******@community.nospam> wrote in message
news:eY**************@TK2MSFTNGP15.phx.gbl...
I'm trying to convert this macro to a c# function but I have a big problem. It's on the LEFT side of an assignment statement and I am extremely
flustered over this one because I'm a little rusty and have been struggling over this for days, not wanting to show all my ignorance in final desparate plea for help which this is ...

Here is the macro ...

#define X(t,b) (sp->x[(t)*sp->beecount+(b)])

sp is a pointer to a structure like this

typedef struct {
int beecount; // beecount is set to 144
int *x // is set to a random pixel X location.
} swarmstruct

x is allocated memory thusly ...

sp->x = (short *) malloc(sizeof(short) * sp->beecount * TIMES); // TIMES is a constant = 4

Later, X is called like this ..
for (b = 0; b < sp->beecount; b++) {
X(0,b) = rand() % width; // width is the screen width.
X(1, b) = X(0, b);
}

What confuses me is that when I try to translate this I get the following
for the first call ...

sp->x[(0)*sp->beecount+(0)]

I am confused by this. If I take this * operator as meaning the indirection operator than what does the (0) I get what appears to make no sense to me
... x[(0)MemLoc(beecount)+0] = a random pixal x location and am trying to
assign a number to an address and treating an address as an indexor?

If I take the * to mean multiply than I am doing x[0 * sp->beecount + b] =
random pixal x location but that is so redundant because it just comes down to x[b] = something.

I am equally confused by the second call which to me looks like it should
create an overflow error.

I do not understand what is happening here. There is no real meaningful
documentation and the author allows the inclusion of the source code as long as I mention his name in the ending program.

I would have put this in the C++ forum here but there ISN'T one, and I'm
trying to convert it to c# using arrays instead of pointers and I'm stuck
bigtime on this point.

Anyone have any hints as to how I should convert or interprety this macro?
I've been trying to interpret it by simply doing the pre-processing inlining by hand since c# has no macro ability but since the macros use pointers and I'm trying to convert to array logic, I haven't a hope of interpreting this until I understand what X is supposed to do.

Thanks for any help. /hangs my head in shame of his ignorance.

Nov 17 '05 #4
Thanks!, That is exactly what I did. I made x a two dimentional array. What
blew me away was when I finished the program and hit run, I expected the
computer to blow chunks, due to all the guessing I had to do, and the
confusion, caused by my having lost the progrmmers reference for Zortech and
the HELP file too, so I had to guess what the program was doing, but when I
finished the screensaver, it worked perfectly. I haven't had that happen in
about 15 years, to have such a huge program, run perfectly the first time.
I've yet to get some details ironed out, but I'll probably be posting it as a
sample.

"James Curran" wrote:
The macro is attempting to simulate a 2 dimentional array: X(a,b)
roughly equals x[a][b]. However, that would require at least one dimension
being fixed. Of course, in this design, one dimension *is* fixed, but I
guess at some point in the design phase TIMES was a variable. In other
words, the C code (and this is C code, not C++) could have been written as:

// type INT_TIMES is an array of 4 integers.
typedef int INT_TIMES[TIMES];
typedef struct {
int beecount:
INT_TIMES* x; // x points to an array of INT_TIMES objects.
}swarmstruct;

sp->beecount = 144;
sp->x = malloc(sp->beecount * sizeof(INT_TIMES));

for (b = 0; b < sp->beecount; b++)
{
// note I had to reverse the indexes here.
sp->x[b][0] = rand() % width;
sp->x[b][1] = sp->x[b][0];
}
Now that we (hopefully) understand what's going on, it should be easy to
translate that into C#:

const int TIMES = 4;
struct swarmstruct
{
int beecount;
int[,] x;
}

sp.beecount = 144;
sp.x = new int[TIMES, sp.beecount]
for (b = 0; b < sp.beecount; b++)
{
// Note, the indexes are back in their original order.
sp.x[0][b] = rand() % width;
sp.x[1][b] = sp->x[0][b];
}

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Garry Freemyer" <Ga******@community.nospam> wrote in message
news:eY**************@TK2MSFTNGP15.phx.gbl...
I'm trying to convert this macro to a c# function but I have a big

problem.
It's on the LEFT side of an assignment statement and I am extremely
flustered over this one because I'm a little rusty and have been

struggling
over this for days, not wanting to show all my ignorance in final

desparate
plea for help which this is ...

Here is the macro ...

#define X(t,b) (sp->x[(t)*sp->beecount+(b)])

sp is a pointer to a structure like this

typedef struct {
int beecount; // beecount is set to 144
int *x // is set to a random pixel X location.
} swarmstruct

x is allocated memory thusly ...

sp->x = (short *) malloc(sizeof(short) * sp->beecount * TIMES); // TIMES

is
a constant = 4

Later, X is called like this ..
for (b = 0; b < sp->beecount; b++) {
X(0,b) = rand() % width; // width is the screen width.
X(1, b) = X(0, b);
}

What confuses me is that when I try to translate this I get the following
for the first call ...

sp->x[(0)*sp->beecount+(0)]

I am confused by this. If I take this * operator as meaning the

indirection
operator than what does the (0) I get what appears to make no sense to me
... x[(0)MemLoc(beecount)+0] = a random pixal x location and am trying to
assign a number to an address and treating an address as an indexor?

If I take the * to mean multiply than I am doing x[0 * sp->beecount + b] =
random pixal x location but that is so redundant because it just comes

down
to x[b] = something.

I am equally confused by the second call which to me looks like it should
create an overflow error.

I do not understand what is happening here. There is no real meaningful
documentation and the author allows the inclusion of the source code as

long
as I mention his name in the ending program.

I would have put this in the C++ forum here but there ISN'T one, and I'm
trying to convert it to c# using arrays instead of pointers and I'm stuck
bigtime on this point.

Anyone have any hints as to how I should convert or interprety this macro?
I've been trying to interpret it by simply doing the pre-processing

inlining
by hand since c# has no macro ability but since the macros use pointers

and
I'm trying to convert to array logic, I haven't a hope of interpreting

this
until I understand what X is supposed to do.

Thanks for any help. /hangs my head in shame of his ignorance.


Nov 17 '05 #5

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

Similar topics

8
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
4
by: dkintheuk | last post by:
Hi there, I want to print a macro out to enable me to debug it and convert it to more sensible operations giving users more control. I get the error message: Application-defined or...
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
9
by: Chen Shu | last post by:
Hi there: These days I came to a problem that I failed to convert a binary number to a hex number using a macro. For example: int a = BINARY4(0101) will become int a = 0x5; int a =...
42
by: Martin Jørgensen | last post by:
Hi, I'm trying to move a matlab program into c language. For those who knows matlab, this is the line I want to program in c: hx(1:nx,1:ny) = 0; % nx=10, ny=10 It works on a 2-dimensional...
5
by: Bill | last post by:
This database has no forms. I am viewing an Access table in datasheet view. I'd like to execute a macro to execute a function (using "runcode"). In the function, I'll reading data from the record...
1
by: rfranzl | last post by:
Hello, I need some help, I have about 200 databases that are copies of an original database that has a similiar table in all of the databases, called "tblCodebook". What I am trying to do is to...
1
by: yimma216 | last post by:
I am working on a macro and try to convert data in a pivot table to a flat data table automatically. Eg. Apple Oragne Pear Tim 1 2 3 Joe 2 3 4 Elle 6 ...
9
by: Marco Nef | last post by:
Hi there I'm looking for a template class that converts the template argument to a string, so something like the following should work: Convert<float>::Get() == "float"; Convert<3>::Get() ==...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.