Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > interesting pointer problem?

Reply
Thread Tools

interesting pointer problem?

 
 
Davy
Guest
Posts: n/a
 
      09-06-2005
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

 
Reply With Quote
 
 
 
 
Lawrence Kirby
Guest
Posts: n/a
 
      09-06-2005
On Tue, 06 Sep 2005 01:40:01 -0700, Davy wrote:

> I found a interesting pointer problem.
>
> What does the pointer mean?
> EXTERN short (*blocks)[64];


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.

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


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
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      09-06-2005
Davy wrote:

> I found a interesting pointer problem.
>
> What does the pointer mean?
> EXTERN short (*blocks)[64];


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.

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


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

 
Reply With Quote
 
Davy
Guest
Posts: n/a
 
      09-06-2005
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

 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      09-06-2005
"Davy" <> wrote in message
news: 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.

>
> I am confused with
> " short (*blocks)[64];"
> and " short [64] (*blocks);"
> and "short *blocks [64];".
>
> What's their difference?


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

 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      09-06-2005
Davy wrote:
>
> Hi,
>
> Thank you for your help!
>
> So the "blocks" is a pointer to a dynamic 2-D array, is it?


No. It is a pointer to an 1D array.

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


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


> and " short [64] (*blocks);"


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


> and "short *blocks [64];".


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

 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      09-06-2005
John Carson wrote:
>
> "Davy" <> wrote in message
> news: 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.


No way. That indexing will not work.

--
Karl Heinz Buchegger

 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      09-06-2005
"Karl Heinz Buchegger" <> wrote in message
news:
> John Carson wrote:
>>
>> "Davy" <> wrote in message
>> news: 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.

>
> No way. That indexing will not work.


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
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      09-06-2005
Karl Heinz Buchegger wrote:

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

>
> No way. That indexing will not work.


Why not?
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      09-06-2005
John Carson wrote:
>
> "Karl Heinz Buchegger" <> wrote in message
> news:
> > John Carson wrote:
> >>
> >> "Davy" <> wrote in message
> >> news: 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.

> >
> > No way. That indexing will not work.

>
> Seems to work fine:
>


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


--
Karl Heinz Buchegger

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57