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

interesting pointer problem?

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

Sep 6 '05 #1
12 1816
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
Sep 6 '05 #2
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'.

Sep 6 '05 #3
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

Sep 6 '05 #4
"Davy" <zh*******@gmail.com> wrote in message
news:11**********************@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.

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

Sep 6 '05 #5
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
kb******@gascad.at
Sep 6 '05 #6
John Carson wrote:

"Davy" <zh*******@gmail.com> wrote in message
news:11**********************@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.


No way. That indexing will not work.

--
Karl Heinz Buchegger
kb******@gascad.at
Sep 6 '05 #7
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?
Sep 6 '05 #8
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:43***************@gascad.at
John Carson wrote:

"Davy" <zh*******@gmail.com> wrote in message
news:11**********************@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.


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
Sep 6 '05 #9
John Carson wrote:

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:43***************@gascad.at
John Carson wrote:

"Davy" <zh*******@gmail.com> wrote in message
news:11**********************@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.


No way. That indexing will not work.


Seems to work fine:


Mea culpa.
Sorry. I read something else in your explanation.
--
Karl Heinz Buchegger
kb******@gascad.at
Sep 6 '05 #10
Rolf Magnus wrote:

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?


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
kb******@gascad.at
Sep 6 '05 #11
John Carson wrote on 06/09/05 :
Seems to work fine:

#include <iostream>


<...>

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
Sep 6 '05 #12
Hi Karl and John,

Thank you all for your generous help! I understand!

Best regards,
Davy

Sep 7 '05 #13

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

Similar topics

6
by: Olaf Martens | last post by:
Greetings! Please consider the following piece of program code (note that I have stripped quite a lot of code here): int foo(void) { unsigned short l_valbuf; // address of this goes to...
23
by: Bruno R. Dias | last post by:
Perhaps it would be interesting to program a virtual machine simulating an ancient computer (such as the pdp-7). Then, it would be rather interesting to code for it (porting gcc to it maybe?). I...
16
by: makko | last post by:
Hello, anyone know how to writre a program that take a commandline formula and prints the calculated result? example; $program 1+(2x3(3/2))-8 reagrds; Makkko
12
by: Davy | last post by:
I found a interesting pointer problem. What does the pointer mean? EXTERN short (*blocks); And what does the malloc mean? blocks = (short (*))malloc(count*sizeof(short )); Any suggestions...
27
by: Frederick Gotham | last post by:
I thought it might be interesting to share experiences of tracking down a subtle or mysterious bug. I myself haven't much experience with tracking down bugs, but there's one in particular which...
22
by: sam_cit | last post by:
Hi Everyone, I have the following structure in my program struct sample { char *string; int string_len; };
6
by: per9000 | last post by:
An interesting/annoying problem. I created a small example to provoke an exception I keep getting. Basically I have a C-struct (Container) with a function-pointer in it. I perform repeated calls...
11
by: onkar.n.mahajan | last post by:
Is it possible to from function call on fly in C programming language ? I am faced with an interesting problem in that I need to take function name and arguments on fly (actually from Database...
126
by: jacob navia | last post by:
Buffer overflows are a fact of life, and, more specifically, a fact of C. All is not lost however. In the book "Value Range Analysis of C programs" Axel Simon tries to establish a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.