473,883 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(s hort) * 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(beeco unt)+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 1927
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******@commun ity.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******@commu nity.nospam> wrote in message
news:eY******** ******@TK2MSFTN GP15.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(s hort) * 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(beeco unt)+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_TIME S));

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******@commu nity.nospam> wrote in message
news:eY******** ******@TK2MSFTN GP15.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(s hort) * 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(beeco unt)+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_TIME S));

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******@commu nity.nospam> wrote in message
news:eY******** ******@TK2MSFTN GP15.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(s hort) * 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(beeco unt)+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
2096
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, I've been playing with Qt in KDE on a Slackware 10.0 system, and following their cookbook exercises, and everything's working, and I have no clue what the code is doing, i.e.. how do I connect my brain to the ascii in the files to the stuff on...
4
2992
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 object-defined error With no other information when i try to do so.
5
3661
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 New System.Diagnostics.Process 'p.Start(MDEPDirStr & "macrun.exe", sPPTOut) p.Start("C:\WINDOWS\SYSTEM32\CALC.EXE") 'p.Start("C:\WINDOWS\SYSTEM32\macrun.exe", sPPTOut)
9
7712
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 = BINARY8(0001,0101) will become int a = 0x15; ...etc. I've written the following macros:
42
3190
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 array (size is 10*10), setting all the values inside the 10*10 matrix to zero. My C-function looks like this:
5
3496
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 the cursor was on in the datasheet at the time I executed the macro. So, the questions are: 1) In the macro, how to I get my hands on the record key or record data of the record the cursor was on in the datasheet at the time I executed the...
1
2004
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 create a macro that will go into each of the 200 databases, run an identical "unmatched query" to find any records that might be in one of the copies of the "tblCodebook". There is a shared primary key in the "tblCodebook" which is a StoryID and...
1
1852
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 7 8 into
9
17606
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() == "3"; Convert<HelloWorld>::Get() == "HelloWorld"; The reason I need this is that in our design every class of a certain
0
9942
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10745
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10848
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10417
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9575
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
4614
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3234
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.