Translating from C to Pascal

J

Jeff Schwab

Julian said:
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.

Hope this helps,
Jeff
 
J

Jack Klein

Julian said:
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
 
J

Julian Maisano

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.
 
T

Thomas Wintschel

Julian Maisano said:
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
 
J

Julian Maisano

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
 
J

Jeff Schwab

Jack said:
Julian said:
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';
}
 
J

Jeff Schwab

Julian said:
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*





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 ('<' '>').
 
K

Kurt Watzka

Jeff said:
Jack said:
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
 
J

Jeff Schwab

Kurt said:
#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?
 
J

John Carson

Jeff Schwab said:
Kurt said:
#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. ]"
 
J

Jeff Schwab

John said:
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?

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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top