"Julian Maisano" <
maisanosys@yahoo.com>wrote in message
Quote
Can anybody help me to translate the following lines to
pascal(delphi)?
I think you need to look at a C (or C++) textbook since there are lots of
details that can trip you up in different examples. Pointer syntax is one of
the trickiest parts of C/C++.
Nevertheless, I will have a go:
Quote
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};
An array called data, comprising 2000 components of type BYTE, with each
component initialised to equal zero.
Quote
BITMAPINFOHEADER *bh=(BITMAPINFOHEADER*)data;
On the left-hand side (LHS) is a pointer called bh, which points to a
BITMAPINFOHEADER structure. On the right-hand side (RHS) is the data
variable encountered previously, which is "cast" to a variable of type
"pointer to BITMAPINFOHEADER", i.e., the RHS is cast to the same type as the
LHS.
data is an array, but there is a built in conversion that converts an array
name into a pointer to the first element of the array. Thus data can be
automatically converted to "pointer to BYTE" and this "pointer to BYTE" is
cast to a "pointer to BITMAPINFOHEADER".
Quote
RGBQUAD *pal=(RGBQUAD*)(data+sizeof(*bh));
On the LHS, pal is a pointer to a RGBQUAD structure. On the RHS, data is
converted to a pointer, which stores a memory address, and the value
sizeof(*bh) is added to the address in order to give a later address. Thus
you end up with a pointer to a later address, which is cast to a pointer to
RGBQUAD.
In the expression sizeof(*bf), *bf refers to the BITMAPINFOHEADER structure
that bf points to. This is the most confusing part of pointer syntax. To
explain further, * is the dereferencing operator. When the dereferencing
operator is applied to a pointer, the result is the object pointed to.
Observe now that the general format for variable declaration is
typename variablename;
Thus in the definition
BITMAPINFOHEADER *bh=(BITMAPINFOHEADER*)data;
you can look at the LHS in either of two ways:
1) BITMAPINFOHEADER is the typename and *bh is the variablename. This says
that *bh is a BITMAPINFOHEADER, which must mean that bh is a pointer to
BITMAPINFOHEADER.
2) BITMAPINFOHEADER * is the typename (i.e., the typename is "pointer to
BITMAPINFOHEADER") and bh is the variablename. Once again, this means that
bh is a pointer to BITMAPINFOHEADER.
What makes this extra tricky is that it is only when a pointer is
initialised at the time of declaration that
*bh = ???
will make an assignment to the pointer. At any other time
*bh = ???
will make an assignment to the BITMAPINFOHEADER object that bh points to.
Thus the RHS will not be a pointer, it will be another BITMAPINFOHEADER
object. To make an assignment to the pointer, you simply use
bh = ???
Quote
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
* follows type names (char, int, RGBQUAD, BITMAPINFOHEADER) and precedes
variable names (bh, pal).
Quote
Neither I understand this kind of lines:
(BITMAPINFO*)bh {a type cast?}
Yes, a type cast.
Quote
&specbuf {the data referenced by a Pointer variable?}
If this is C (and not C++), then & is the "address of" operator. &specbuf
gives the address in memory of the specbuf variable.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
-