473,395 Members | 1,936 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,395 software developers and data experts.

Translating from C to Pascal

Can anybody help me to translate the following lines to pascal(delphi)?

Let me explain what I'm looking for:

int i = 1; {In this line, an integer variable which is called "i" is
declared and it is initialised with 1}
BYTE data[2000]={0}; {your comments here...}
BITMAPINFOHEADER *bh=(BITMAPINFOHEADER*)data; {your comments here...}
RGBQUAD *pal=(RGBQUAD*)(data+sizeof(*bh)); {your comments here...}
I am very confused with the meaning that has the * in C/C++
I don't understand why it precedes the variable/type in some cases and in
other cases it is later
Neither I understand this kind of lines:

(BITMAPINFO*)bh {a type cast?}
&specbuf {the data referenced by a Pointer variable?}
TIA.
Jul 22 '05 #1
10 1844
Julian Maisano wrote:
Can anybody help me to translate the following lines to pascal(delphi)?

Let me explain what I'm looking for:

int i = 1; {In this line, an integer variable which is called "i" is
declared and it is initialised with 1}
BYTE data[2000]={0}; {your comments here...}
Create an array of 2000 elements (of type BYTE) on the stack. Set the
first element to 0. That may not be what the author meant, but that's
probably what he got.
BITMAPINFOHEADER *bh=(BITMAPINFOHEADER*)data; {your comments here...}
Pointers are variables that hold memory addresses. This line defines a
pointer (called "bh") to hold the address of an existing variable called
"data." This is an ancient, and particularly unhelpful, style of C++.
RGBQUAD *pal=(RGBQUAD*)(data+sizeof(*bh)); {your comments here...}
This seems to define another pointer ("pal") to point somewere in the
middle of "data." The author may have been trying to access individual
members of an aggregate data structure. This does not appear to be very
good code.
I am very confused with the meaning that has the * in C/C++
I don't understand why it precedes the variable/type in some cases and in
other cases it is later
I'm sorry to have to tell you this, but C++ syntax blows. Most of it
was inherited from C. Syntax is generally considered the one big weak
spot of C++.
Neither I understand this kind of lines:

(BITMAPINFO*)bh {a type cast?}
You got it. That line means "let's pretend that bh is a pointer holding
the address of some BITMAPINFO, whether it is not."
&specbuf {the data referenced by a Pointer variable?}
Close. The "&", or "address-of" operator, gives the location of a
variable in memory.
TIA.


Hope this helps,
Jeff

Jul 22 '05 #2
First of all
Thanks. You helped me a lot

I have another question:

Are these lines equivalent in C++?

whatevertype* name

*whatevertype name

whatevertype *name

whatevertype name*

I'm sorry to have to tell you this, but C++ syntax blows. Most of it
was inherited from C. Syntax is generally considered the one big weak
spot of C++.


mmmm,,,,
What you say has a lot of relevance, because according to what I was looking
in another posts, it is seen that you are a great expert.

Julian Maisano, La Plata, Arg
Jul 22 '05 #3
On Tue, 30 Dec 2003 23:50:52 -0500, Jeff Schwab <je******@comcast.net>
wrote in comp.lang.c++:
Julian Maisano wrote:
Can anybody help me to translate the following lines to pascal(delphi)?

Let me explain what I'm looking for:

int i = 1; {In this line, an integer variable which is called "i" is
declared and it is initialised with 1}
BYTE data[2000]={0}; {your comments here...}


Create an array of 2000 elements (of type BYTE) on the stack. Set the
first element to 0. That may not be what the author meant, but that's
probably what he got.


Sets all 2000 "BYTE" elements to 0.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 22 '05 #4
"Julian Maisano" <ma********@yahoo.com> wrote in message
news:bs**********@newsreader.mailgate.org...
First of all
Thanks. You helped me a lot

I have another question:

Are these lines equivalent in C++?

whatevertype* name

*whatevertype name

whatevertype *name

whatevertype name*


whatevertype* name;
and
whatevertype *name;
are equivalent. They both declare 'name' to be a pointer to 'whatevertype'.

The others two will generate errors.
'whatevertype name' declares 'name' to be an object of type
'whatevername'.
'*;' is an attempt to dereference a semicolon.
'*whatevertype' is an attempt to dereference a type name.

Dereference (see indirection) being a non-intuitive term, I offer the
following quick example.

int x = 1; // declares an integer and assigns it the value 1
int *py = &x; // declares a pointer to int and assigns it the address of x
int z = *py // declares an integer and assigns it the value that py points
to,
in this case, the current value of x.

Tom
Jul 22 '05 #5
Jack Klein wrote:
On Tue, 30 Dec 2003 23:50:52 -0500, Jeff Schwab <je******@comcast.net>
wrote in comp.lang.c++:

Julian Maisano wrote:
Can anybody help me to translate the following lines to pascal(delphi)?

Let me explain what I'm looking for:

int i = 1; {In this line, an integer variable which is called "i" is
declared and it is initialised with 1}
BYTE data[2000]={0}; {your comments here...}


Create an array of 2000 elements (of type BYTE) on the stack. Set the
first element to 0. That may not be what the author meant, but that's
probably what he got.

Sets all 2000 "BYTE" elements to 0.


On what compiler, or according to what document? Are you sure your
compiler doesn't just initialize some arrays to all zero's by default?
Try this:
#include <iostream>

int main( )
{
int data[ 2 ] = { 1 };

std::cout << data[ 0 ] << '\n'
<< data[ 1 ] << '\n';
}

Jul 22 '05 #6
Julian Maisano wrote:
First of all
Thanks. You helped me a lot

I have another question:

Are these lines equivalent in C++?

whatevertype* name

*whatevertype name

whatevertype *name

whatevertype name*
I'm sorry to have to tell you this, but C++ syntax blows. Most of it
was inherited from C. Syntax is generally considered the one big weak
spot of C++.

mmmm,,,,
What you say has a lot of relevance, because according to what I was looking
in another posts, it is seen that you are a great expert.

Julian Maisano, La Plata, Arg


If by "great expert," you mean "talks a lot," then I qualify.
Otherwise, I'm afraid I do not. ;)

The code you posted earlier was in the style of the C programming
language. If most of the code you must read looks similar(1), you may
find this book a great help:

The C Programming Language
Second Edition
Brian W. Kernighan and Dennis M. Ritchie

It will get you up to speed on the syntax very quickly, and it has a
wonderful index and appendices I still use for reference.

Of course, Tom's explanation of pointer syntax may be all you need. :)

-Jeff

(1) Words with lots of parentheses, but very few lines that look like
"letters.different_letters( )" and not too many angle brackets ('<' '>').

Jul 22 '05 #7
Jeff Schwab wrote:
Jack Klein wrote:
On Tue, 30 Dec 2003 23:50:52 -0500, Jeff Schwab <je******@comcast.net>
wrote in comp.lang.c++:

Julian Maisano wrote:

Can anybody help me to translate the following lines to pascal(delphi)?

Let me explain what I'm looking for:

int i = 1; {In this line, an integer variable which is called "i" is
declared and it is initialised with 1}
BYTE data[2000]={0}; {your comments here...}

Create an array of 2000 elements (of type BYTE) on the stack. Set the
first element to 0. That may not be what the author meant, but that's
probably what he got.

Sets all 2000 "BYTE" elements to 0.


On what compiler, or according to what document?


On all non-broken compilers according to the language definition.
Are you sure your
compiler doesn't just initialize some arrays to all zero's by default?
Try this:
#include <iostream>

int main( )
{
int data[ 2 ] = { 1 };

std::cout << data[ 0 ] << '\n'
<< data[ 1 ] << '\n';
}


If data[1] is not 0, the compiler is broken.

Kurt Watzka

Jul 22 '05 #8
Kurt Watzka wrote:
#include <iostream>

int main( )
{
int data[ 2 ] = { 1 };

std::cout << data[ 0 ] << '\n'
<< data[ 1 ] << '\n';
}

If data[1] is not 0, the compiler is broken.


On my compiler, data[ 1 ] is zero. Which does not match the initializer
{ 1 }. So do you still think the initializer applies to all elements of
the array?

Jul 22 '05 #9
"Jeff Schwab" <je******@comcast.net> wrote in message
news:6d********************@comcast.com
Kurt Watzka wrote:
#include <iostream>

int main( )
{
int data[ 2 ] = { 1 };

std::cout << data[ 0 ] << '\n'
<< data[ 1 ] << '\n';
}

If data[1] is not 0, the compiler is broken.


On my compiler, data[ 1 ] is zero. Which does not match the
initializer { 1 }. So do you still think the initializer applies to
all elements of the array?

The rule is not that the first initialiser is repeated. The rule is that
anything not explicitly initialised is default-intialised, which means set
equal to zero in the case of an int. If the first and only initialiser
happens to be zero, then this means that everything is set to zero.

The matter is covered in the C++ standard in section 8.5. First we have the
definition of "default-initialisation":

"To default-initialize an object of type T means:
- if T is a non-POD class type (clause 9), the default constructor for T is
called (and the initialization is
ill-formed if T has no accessible default constructor);
- if T is an array type, each element is default-initialized;
- otherwise, the storage for the object is zero-initialized."

Next we have the definition of an "aggregate".

"An aggregate is an array or a class (clause 9) with no user-declared
constructors (12.1), no private or pro-tected
non-static data members (clause 11), no base classes (clause 10), and no
virtual functions (10.3)."

Finally, we have the relevant rules on the default intialisation of an
aggregate:

"If there are fewer initializers in the list than there are members in the
aggregate, then each member not
explicitly initialized shall be default-initialized (8.5).
[Example:
struct S { int a; char* b; int c; };
S ss = { 1, "asdf" };
initializes ss.a with 1, ss.b with "asdf", and ss.c with the value of an
expression of the form
int(), that is, 0. ]"

--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #10
John Carson wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:6d********************@comcast.com
Kurt Watzka wrote:

#include <iostream>

int main( )
{
int data[ 2 ] = { 1 };

std::cout << data[ 0 ] << '\n'
<< data[ 1 ] << '\n';
}
If data[1] is not 0, the compiler is broken.
On my compiler, data[ 1 ] is zero. Which does not match the
initializer { 1 }. So do you still think the initializer applies to
all elements of the array?


<snip/>
Finally, we have the relevant rules on the default intialisation of an
aggregate:

"If there are fewer initializers in the list than there are members in the
aggregate, then each member not
explicitly initialized shall be default-initialized (8.5).
[Example:
struct S { int a; char* b; int c; };
S ss = { 1, "asdf" };
initializes ss.a with 1, ss.b with "asdf", and ss.c with the value of an
expression of the form
int(), that is, 0. ]"


That's the bit I was missing. Please (continue to) correct me if I'm wrong:

Providing an initializer for only the first part of an automatic
array causes default initialization of the remaining elements.

This never would have occurred to me. I thought the initial values of
automatic variables of built-in types were implementation dependent,
unless initialization was specified explicitly by the programmer.
Thanks for teaching me something new(1)!

-Jeff

(1) Well, new to me, anyway.

Jul 22 '05 #11

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

Similar topics

13
by: cjl | last post by:
Hey all: I'm working on a 'pure' python port of some existing software. Implementations of what I'm trying to accomplish are available (open source) in C++ and in Java. Which would be...
4
by: Chris Gordon-Smith | last post by:
I am tying to call a Pascal function from C++, and vice versa. Does anyone know how to do this, or where detailed information on this topic can be found? For the C++ to Pascal call I have...
24
by: Faith Dorell | last post by:
I really don´t like C.You can write better programs in BASIC than in C, if you don´t like this language. I don´t understand how C became so popular, although much better programming languages...
28
by: Skybuck Flying | last post by:
Hi, I think I understand now a bit better what the difference is between a c compiler and a pascal compiler. For example: When compiling source code with a pascal compiler. The pascal...
17
by: David Scemama | last post by:
Hi, I'm writing a program using VB.NET that needs to communicate with a DOS Pascal program than cannot be modified. The communication channel is through some file databases, and I have a huge...
15
by: jacob navia | last post by:
Programming languages come and go. Still is amazing that in this survey from http://www.devsource.com/article2/0,1895,2016936,00.asp the C language comes second, right after Java. Java # What...
0
by: dhruba.bandopadhyay | last post by:
Am using Borland C++ 4.5 for the old dos.h APIs. It appears that newer versions of compilers stop support for the oldskool DOS routines. Am trying to convert/port an oldskool Pascal program that...
7
by: SMALLp | last post by:
Hy! I desperately need help! I need to make application that would accept Pascal code and check if it returns good results. My idea is (from a beginner point of view) to make application in...
54
by: Ruud | last post by:
Hallo allemaal, During the conversion of my program from Pascal to C, I was more or less able to find the C equivalent of most Pascal functions so far. Only four gave me some real trouble. I...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
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,...

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.