Connecting Tech Pros Worldwide Help | Site Map

interesting pointer problem?

Davy
Guest
 
Posts: n/a
#1: Sep 6 '05
I found a interesting pointer problem.

What does the pointer mean?
EXTERN short (*blocks)[64];

And what does the malloc mean?
blocks =
(short (*)[64])malloc(count*sizeof(short [64]));

Any suggestions will be appreciated!
Best regards,
Davy

Lawrence Kirby
Guest
 
Posts: n/a
#2: Sep 6 '05

re: interesting pointer problem?


On Tue, 06 Sep 2005 01:40:01 -0700, Davy wrote:
[color=blue]
> I found a interesting pointer problem.
>
> What does the pointer mean?
> EXTERN short (*blocks)[64];[/color]

EXTERN isn't a standard identifier, it is probably defined as a macro
somewhere expanding to extern or nothing.

blocks is a pointer to an array of 64 shorts.
[color=blue]
> And what does the malloc mean?
> blocks =
> (short (*)[64])malloc(count*sizeof(short [64]));[/color]

This allocates in effect a 2D array (in fact an array of arrays) of count
* 64 shorts whose elements can be accessed using blocks[a][b]. Assuming
the malloc() call succeeds of course.

Note this is a case where C and C++ have different approaches. In C you
would tend to use the malloc() call without the cast, in C++ you would
tend to use the new operator.

Lawrence
Rolf Magnus
Guest
 
Posts: n/a
#3: Sep 6 '05

re: interesting pointer problem?


Davy wrote:
[color=blue]
> I found a interesting pointer problem.
>
> What does the pointer mean?
> EXTERN short (*blocks)[64];[/color]

Assuming EXTERN is just #defined to be the same as extern, it means that a
variable named 'blocks' is declared (not defined) that is a pointer to an
array of 64 short values.
[color=blue]
> And what does the malloc mean?
> blocks =
> (short (*)[64])malloc(count*sizeof(short [64]));[/color]

It allocates 'count' arrays of 64 short and assigns the address of the first
one to 'blocks'.

Davy
Guest
 
Posts: n/a
#4: Sep 6 '05

re: interesting pointer problem?


Hi,

Thank you for your help!

So the "blocks" is a pointer to a dynamic 2-D array, is it?

I am confused with
" short (*blocks)[64];"
and " short [64] (*blocks);"
and "short *blocks [64];".

What's their difference?

Best regards,
Davy

John Carson
Guest
 
Posts: n/a
#5: Sep 6 '05

re: interesting pointer problem?


"Davy" <zhushenli@gmail.com> wrote in message
news:1126010573.382090.155720@g14g2000cwa.googlegr oups.com[color=blue]
> Hi,
>
> Thank you for your help!
>
> So the "blocks" is a pointer to a dynamic 2-D array, is it?[/color]

It is a pointer to a 1 dimensional array of 64 shorts. Of course, it you
allocate several of these, one after the other, and make blocks point to the
first of them, then it is pointing to the first row of a 2D array. blocks[0]
will get row 0, blocks[1] will get the row 1 and so on. blocks[i][j] will
get the (i,j)th element of a 2D array.
[color=blue]
>
> I am confused with
> " short (*blocks)[64];"
> and " short [64] (*blocks);"
> and "short *blocks [64];".
>
> What's their difference?[/color]

The first has been explained, the second is not valid C++ at all, and the
second is an ordinary array comprising 64 pointers to short.

--
John Carson

Karl Heinz Buchegger
Guest
 
Posts: n/a
#6: Sep 6 '05

re: interesting pointer problem?


Davy wrote:[color=blue]
>
> Hi,
>
> Thank you for your help!
>
> So the "blocks" is a pointer to a dynamic 2-D array, is it?[/color]

No. It is a pointer to an 1D array.
[color=blue]
>
> I am confused with
> " short (*blocks)[64];"[/color]

start at the variable name. Then read to the right until you reach
a ')' or ']'. Continue reading at the left until you hit a '(' or '['
where you continue to the right and so on. (This is called the left/right rule)

blocks blocks is
blocks) oops, stop reading right, continue left
*blocks) blocks is a pointer
(*blocks) oops, stop reading left, continue to the right
(*blocks)[ blocks is a pointer to an array
(*blocks)[64 blocks is a pointer to an array of size 64
(*blocks)[64] oops, stop reading right, continue to the left
short (*blocks)[64] blocks is a pointer to an array of size 64 of short

[color=blue]
> and " short [64] (*blocks);"[/color]

blocks blocks is
blocks) -> left
*blocks) blocks is a pointer
(*blocks) -> right
(*blocks) nothing on the right, continue left
](*blocks) syntax error, there cannot be a '[' at this position

[color=blue]
> and "short *blocks [64];".[/color]

blocks blocks is
blocks [ blocks is an array
blocks [64 blocks is an array of size 64
blocks [64] -> right
* blocks [64] blocks is an array of size 64 of pointer
short * blocks [64] blocks is an array of size 64 of pointer to short



So the first one (used in a program) can be used to build up this data structure

blocks
+---------+ +-------+ ----+
| o----------------->| | |
+---------+ +-------+ |
| | |
^ +-------+
| | | 64 short's
| ...
| | | |
| +-------+ |
| | | |
| +-------+ ----+
|
| ^
| |
| Array of short
|
Pointer to Array of short's

While the third one, can be used to build this:


blocks
+--------+ +-----+
| o-------------------------->| |
+--------+ +-----+ +-----+
| o----------------->| |
+--------+ +-----+ +-----+
| o----------------------------->| |
..... +-----+ +-----+
| o-------------------->| |
+--------+ +-----+ +-----+
| o------------------------------->| |
+--------+ +-----+

^
|
An array of pointers, where each pointer points to a short


--
Karl Heinz Buchegger
kbuchegg@gascad.at
Karl Heinz Buchegger
Guest
 
Posts: n/a
#7: Sep 6 '05

re: interesting pointer problem?


John Carson wrote:[color=blue]
>
> "Davy" <zhushenli@gmail.com> wrote in message
> news:1126010573.382090.155720@g14g2000cwa.googlegr oups.com[color=green]
> > Hi,
> >
> > Thank you for your help!
> >
> > So the "blocks" is a pointer to a dynamic 2-D array, is it?[/color]
>
> It is a pointer to a 1 dimensional array of 64 shorts. Of course, it you
> allocate several of these, one after the other, and make blocks point to the
> first of them, then it is pointing to the first row of a 2D array. blocks[0]
> will get row 0, blocks[1] will get the row 1 and so on. blocks[i][j] will
> get the (i,j)th element of a 2D array.[/color]

No way. That indexing will not work.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Rolf Magnus
Guest
 
Posts: n/a
#8: Sep 6 '05

re: interesting pointer problem?


Karl Heinz Buchegger wrote:
[color=blue][color=green]
>> It is a pointer to a 1 dimensional array of 64 shorts. Of course, it you
>> allocate several of these, one after the other, and make blocks point to
>> the first of them, then it is pointing to the first row of a 2D array.
>> blocks[0] will get row 0, blocks[1] will get the row 1 and so on.
>> blocks[i][j] will get the (i,j)th element of a 2D array.[/color]
>
> No way. That indexing will not work.[/color]

Why not?
John Carson
Guest
 
Posts: n/a
#9: Sep 6 '05

re: interesting pointer problem?


"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
news:431D986E.D2037182@gascad.at[color=blue]
> John Carson wrote:[color=green]
>>
>> "Davy" <zhushenli@gmail.com> wrote in message
>> news:1126010573.382090.155720@g14g2000cwa.googlegr oups.com[color=darkred]
>>> Hi,
>>>
>>> Thank you for your help!
>>>
>>> So the "blocks" is a pointer to a dynamic 2-D array, is it?[/color]
>>
>> It is a pointer to a 1 dimensional array of 64 shorts. Of course, it
>> you allocate several of these, one after the other, and make blocks
>> point to the first of them, then it is pointing to the first row of
>> a 2D array. blocks[0] will get row 0, blocks[1] will get the row 1
>> and so on. blocks[i][j] will get the (i,j)th element of a 2D array.[/color]
>
> No way. That indexing will not work.[/color]

Seems to work fine:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
const size_t rowcount = 3;
const size_t colcount = 16;

short (*blocks)[colcount];

blocks = (short (*)[colcount])
malloc(rowcount*sizeof(short [colcount]));

for(size_t i=0; i<rowcount; ++i)
{
for(size_t j=0; j<colcount; ++j)
blocks[i][j] = i*100 + j;
}
for(size_t i=0; i<rowcount; ++i)
{
for(size_t j=0; j<colcount; ++j)
cout << setw(3) << blocks[i][j] << ' ';
cout << endl;
}
free(blocks);
}


--
John Carson
Karl Heinz Buchegger
Guest
 
Posts: n/a
#10: Sep 6 '05

re: interesting pointer problem?


John Carson wrote:[color=blue]
>
> "Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
> news:431D986E.D2037182@gascad.at[color=green]
> > John Carson wrote:[color=darkred]
> >>
> >> "Davy" <zhushenli@gmail.com> wrote in message
> >> news:1126010573.382090.155720@g14g2000cwa.googlegr oups.com
> >>> Hi,
> >>>
> >>> Thank you for your help!
> >>>
> >>> So the "blocks" is a pointer to a dynamic 2-D array, is it?
> >>
> >> It is a pointer to a 1 dimensional array of 64 shorts. Of course, it
> >> you allocate several of these, one after the other, and make blocks
> >> point to the first of them, then it is pointing to the first row of
> >> a 2D array. blocks[0] will get row 0, blocks[1] will get the row 1
> >> and so on. blocks[i][j] will get the (i,j)th element of a 2D array.[/color]
> >
> > No way. That indexing will not work.[/color]
>
> Seems to work fine:
>[/color]

Mea culpa.
Sorry. I read something else in your explanation.


--
Karl Heinz Buchegger
kbuchegg@gascad.at
Karl Heinz Buchegger
Guest
 
Posts: n/a
#11: Sep 6 '05

re: interesting pointer problem?


Rolf Magnus wrote:[color=blue]
>
> Karl Heinz Buchegger wrote:
>[color=green][color=darkred]
> >> It is a pointer to a 1 dimensional array of 64 shorts. Of course, it you
> >> allocate several of these, one after the other, and make blocks point to
> >> the first of them, then it is pointing to the first row of a 2D array.
> >> blocks[0] will get row 0, blocks[1] will get the row 1 and so on.
> >> blocks[i][j] will get the (i,j)th element of a 2D array.[/color]
> >
> > No way. That indexing will not work.[/color]
>
> Why not?[/color]

I made a mistake. Somehow I mixed the rows with the columns when thinking
through and came to the conclusion that ....

But John is fine. It will work.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Emmanuel Delahaye
Guest
 
Posts: n/a
#12: Sep 6 '05

re: interesting pointer problem?


John Carson wrote on 06/09/05 :[color=blue]
> Seems to work fine:
>
> #include <iostream>[/color]

<...>

Not a C-code.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC


Davy
Guest
 
Posts: n/a
#13: Sep 7 '05

re: interesting pointer problem?


Hi Karl and John,

Thank you all for your generous help! I understand!

Best regards,
Davy

Closed Thread