array pointer/pointer array
Author Message
array pointer/pointer array

Dear all,

int y;
double *ptr[30][6];

For me, it is an pointer array with 180 pointers in it.
I was told that * means it is an array of y length.
Does it mean taht *ptr[30][6] can be ptr[y][30][6] or ptr
[30][6][y]?
If it can represent a 3D array, how can I allocate
memeory to *ptr[30][6] and make it to ptr[y][30][6]?

Wed, 21 Sep 2005 19:54:07 GMT
array pointer/pointer array
Lawrence,

I am not entirely sure what you're wanting to accomplish here, so let
me re-state my interpretation of your question and, if I get it wrong,
please tell me, and I will try and correct the answer. :)

First, it sonds like you are wanting to create a 3-dimensional array,
but you want to use a variable for at least one of the dimensions, so
that you can create an array of an indeterminate size at run-time?  I
am assuming this becuase your use of int y for one of the dimensions

C++ uses two kinds of storage:  Static and dynamic.  Static storage is
storage that C++ knows about and sets up at compile time.  Dynamic
storage is storage that C++ doesn't know about at compile time, but
allocates instead "on the fly" at run time as its needed.  Arrays are
an example of statically allocated storage.  That is, C++ needs to
know exactly how much space the array will take up when the program
compiles.  For this reason, you cannot have an array of a variable
length.  You would create a three dimensional array of pointers like
this:

double *ptrArray[10][30][60]; // Array of 1800 pointers to double's

You do have some options if you really "need" to create an array of
variable lenght.  One thing you could do is to create a class (call it
a "node" or something) and make a linked list out of it.  Include a
static member of type node* called pRoot, and a non-static member
pRoot* pNext.  Overload the [] operator to return a reference to the
requested node.  Use the new() operator to add new noes to the list.
If you want help with this technique. let me know.  It's quite easy
once you have the hang of it.

Alternately, if you are using MS Visual C++ and MFC, you can use
something called the COleSafeArray.  Safe Arrays are basically arrays
of COleVAriant's and they can be of an arbitrary type and length.

Wow....  I sure hope I answered your question, but again if not,
please let me know and I'll try again. :)

-- Jim

On Sat, 5 Apr 2003 03:54:07 -0800, "Lawrence"

Quote:

>Dear all,

>int y;
>double *ptr[30][6];

>For me, it is an pointer array with 180 pointers in it.
>I was told that * means it is an array of y length.
>Does it mean taht *ptr[30][6] can be ptr[y][30][6] or ptr
>[30][6][y]?
>If it can represent a 3D array, how can I allocate
>memeory to *ptr[30][6] and make it to ptr[y][30][6]?

Wed, 21 Sep 2005 23:18:16 GMT
array pointer/pointer array
[Top-posting undone.]

Quote:
> On Sat, 5 Apr 2003 03:54:07 -0800, "Lawrence"

> >Dear all,

> >int y;
> >double *ptr[30][6];

> >For me, it is an pointer array with 180 pointers in it.

Yes, that is a correct reading of the above code.

Quote:
> >I was told that * means it is an array of y length.

Nothing in the posted code suggests that.

Quote:
> >Does it mean taht *ptr[30][6] can be ptr[y][30][6]

No.

Quote:
> >or ptr [30][6][y]?

Not quite.  That expression references past
the bounds of the array as defined.

However, if the pointer ptr[I][j] is indexed
via y, (as ptr[I][j][y]), where at least (y+1)
doubles were allocated at the memory block
to which ptr[I][j] points, (implying that a
new[] or other allocation was done previously),
then that would work.

Quote:
> >If it can represent a 3D array, how can I allocate
> >memeory to *ptr[30][6] and make it to ptr[y][30][6]?

You would be better off with alternatives as I
have mentioned below.  If you are determined to
do this with bare-metal C'ish code, then you
should study up on the relationship between
array expressions and pointer arithmetic.  That
is too vast a topic to repeat here when it is
covered so well in C references.

Quote:
> Lawrence,

> I am not entirely sure what you're wanting to accomplish here, so let
> me re-state my interpretation of your question and, if I get it wrong,
> please tell me, and I will try and correct the answer. :)

> First, it sonds like you are wanting to create a 3-dimensional array,
> but you want to use a variable for at least one of the dimensions, so
> that you can create an array of an indeterminate size at run-time?

I imagine it becomes determinate at runtime. <g>

Quote:
> I
> am assuming this becuase your use of int y for one of the dimensions

> C++ uses two kinds of storage:  Static and dynamic.  Static storage is
> storage that C++ knows about and sets up at compile time.  Dynamic
> storage is storage that C++ doesn't know about at compile time, but
> allocates instead "on the fly" at run time as its needed.  Arrays are
> an example of statically allocated storage.  That is, C++ needs to
> know exactly how much space the array will take up when the program
> compiles.  For this reason, you cannot have an array of a variable
> length.  You would create a three dimensional array of pointers like
> this:

> double *ptrArray[10][30][60]; // Array of 1800 pointers to double's

> You do have some options if you really "need" to create an array of
> variable lenght.  One thing you could do is to create a class (call it
> a "node" or something) and make a linked list out of it.  Include a
> static member of type node* called pRoot, and a non-static member
> pRoot* pNext.  Overload the [] operator to return a reference to the
> requested node.

Warning:  Do not use this technique unless
you are quite sure that O(n) access is going
to be satisfactory for your application.

Quote:
> Use the new() operator to add new noes to the list.
> If you want help with this technique. let me know.  It's quite easy
> once you have the hang of it.

> Alternately, if you are using MS Visual C++ and MFC, you can use
> something called the COleSafeArray.  Safe Arrays are basically arrays
> of COleVAriant's and they can be of an arbitrary type and length.

I would avoid SAFEARRAY and its kin unless
you need COM interoperability.

A few other alternatives are:
- std::vector, nested as necessary to get
the required number of dimensions.
- std::valarray for the fixed dimensions
- boost::multi_array (at www.boost.org)
The latter is the easiest once the fixed
overhead of starting to use the Boost
library is incurred.  That will yield many
other benefits over time.

Quote:
> Wow....  I sure hope I answered your question, but again if not,
> please let me know and I'll try again. :)

> -- Jim

--
-Larry Brasfield

Thu, 22 Sep 2005 01:14:20 GMT
array pointer/pointer array
Thank you.
I want to assign a length y to ptr[30][6] in runtime.
Can I allocate memory of ptr like normal 3D array?
Can I assign values to ptr like:
int a,ba,c;
int y;
y=20; //for example
for (a=0;a<y;a++)
{
for (b=0;b<30;b++)
{
for (c=0;c<6;c++)
{
*(ptr+a*b+c)=values;
}
}
Quote:
}

?
Can I pass the values of ptr like:
myval=*(ptr+1*1+1);
?
Can I convert *ptr[30][6] to ptr[y][30][6] in order to
access the values in ptr like an array earily rather than
arithmetic method?
Thank you.

Quote:
>-----Original Message-----
>[Top-posting undone.]

>> On Sat, 5 Apr 2003 03:54:07 -0800, "Lawrence"

>> >Dear all,

>> >int y;
>> >double *ptr[30][6];

>> >For me, it is an pointer array with 180 pointers in
it.

>Yes, that is a correct reading of the above code.

>> >I was told that * means it is an array of y length.

>Nothing in the posted code suggests that.

>> >Does it mean taht *ptr[30][6] can be ptr[y][30][6]

>No.

>> >or ptr [30][6][y]?

>Not quite.  That expression references past
>the bounds of the array as defined.

>However, if the pointer ptr[I][j] is indexed
>via y, (as ptr[I][j][y]), where at least (y+1)
>doubles were allocated at the memory block
>to which ptr[I][j] points, (implying that a
>new[] or other allocation was done previously),
>then that would work.

>> >If it can represent a 3D array, how can I allocate
>> >memeory to *ptr[30][6] and make it to ptr[y][30][6]?

>You would be better off with alternatives as I
>have mentioned below.  If you are determined to
>do this with bare-metal C'ish code, then you
>should study up on the relationship between
>array expressions and pointer arithmetic.  That
>is too vast a topic to repeat here when it is
>covered so well in C references.

>> Lawrence,

>> I am not entirely sure what you're wanting to

accomplish here, so let

- Show quoted text -

Quote:
>> me re-state my interpretation of your question and, if
I get it wrong,
>> please tell me, and I will try and correct the

>> First, it sonds like you are wanting to create a 3-
dimensional array,
>> but you want to use a variable for at least one of the
dimensions, so
>> that you can create an array of an indeterminate size
at run-time?

>I imagine it becomes determinate at runtime. <g>

>> I
>> am assuming this becuase your use of int y for one of
the dimensions

>> C++ uses two kinds of storage:  Static and dynamic.
Static storage is
>> storage that C++ knows about and sets up at compile
time.  Dynamic
>> storage is storage that C++ doesn't know about at
compile time, but
>> allocates instead "on the fly" at run time as its
needed.  Arrays are
>> an example of statically allocated storage.  That is,
C++ needs to
>> know exactly how much space the array will take up
when the program
>> compiles.  For this reason, you cannot have an array
of a variable
>> length.  You would create a three dimensional array of
pointers like
>> this:

>> double *ptrArray[10][30][60]; // Array of 1800

pointers to double's

- Show quoted text -

Quote:

>> You do have some options if you really "need" to
create an array of
>> variable lenght.  One thing you could do is to create
a class (call it
>> a "node" or something) and make a linked list out of
it.  Include a
>> static member of type node* called pRoot, and a non-
static member
>> pRoot* pNext.  Overload the [] operator to return a
reference to the
>> requested node.

>Warning:  Do not use this technique unless
>you are quite sure that O(n) access is going
>to be satisfactory for your application.

>> Use the new() operator to add new noes to the list.
>> If you want help with this technique. let me know.
It's quite easy
>> once you have the hang of it.

>> Alternately, if you are using MS Visual C++ and MFC,
you can use
>> something called the COleSafeArray.  Safe Arrays are
basically arrays
>> of COleVAriant's and they can be of an arbitrary type
and length.

>I would avoid SAFEARRAY and its kin unless
>you need COM interoperability.

>A few other alternatives are:
>- std::vector, nested as necessary to get
>  the required number of dimensions.
>- std::valarray for the fixed dimensions
>- boost::multi_array (at www.boost.org)
>The latter is the easiest once the fixed
>overhead of starting to use the Boost
>library is incurred.  That will yield many
>other benefits over time.

again if not,
>> please let me know and I'll try again. :)

>> -- Jim

>--
>-Larry Brasfield
>.

Thu, 22 Sep 2005 05:08:43 GMT
array pointer/pointer array
Thank you.
I want to assign a length y to ptr[30][6] in runtime.
Can I allocate memory of ptr like normal 3D array?
Can I assign values to ptr like:
int a,ba,c;
int y;
y=20; //for example
for (a=0;a<y;a++)
{
for (b=0;b<30;b++)
{
for (c=0;c<6;c++)
{
*(ptr+a*b+c)=values;
}
}
Quote:
}

?
Can I pass the values of ptr like:
myval=*(ptr+1*1+1);
?
Can I convert *ptr[30][6] to ptr[y][30][6] in order to
access the values in ptr like an array earily rather than
arithmetic method?
Thank you.

Quote:
>-----Original Message-----
>Lawrence,

>I am not entirely sure what you're wanting to accomplish
here, so let
>me re-state my interpretation of your question and, if I
get it wrong,
>please tell me, and I will try and correct the answer. :)

>First, it sonds like you are wanting to create a 3-
dimensional array,
>but you want to use a variable for at least one of the
dimensions, so
>that you can create an array of an indeterminate size at
run-time?  I
>am assuming this becuase your use of int y for one of
the dimensions

>C++ uses two kinds of storage:  Static and dynamic.
Static storage is
>storage that C++ knows about and sets up at compile
time.  Dynamic
>storage is storage that C++ doesn't know about at
compile time, but
>allocates instead "on the fly" at run time as its
needed.  Arrays are
>an example of statically allocated storage.  That is,
C++ needs to
>know exactly how much space the array will take up when
the program
>compiles.  For this reason, you cannot have an array of
a variable
>length.  You would create a three dimensional array of
pointers like
>this:

>double *ptrArray[10][30][60]; // Array of 1800 pointers
to double's

>You do have some options if you really "need" to create
an array of
>variable lenght.  One thing you could do is to create a
class (call it
>a "node" or something) and make a linked list out of
it.  Include a
>static member of type node* called pRoot, and a non-
static member
>pRoot* pNext.  Overload the [] operator to return a
reference to the
>requested node.  Use the new() operator to add new noes
to the list.
>If you want help with this technique. let me know.  It's
quite easy
>once you have the hang of it.

>Alternately, if you are using MS Visual C++ and MFC, you
can use
>something called the COleSafeArray.  Safe Arrays are
basically arrays
>of COleVAriant's and they can be of an arbitrary type
and length.

if not,
>please let me know and I'll try again. :)

>-- Jim

>On Sat, 5 Apr 2003 03:54:07 -0800, "Lawrence"

>>Dear all,

>>int y;
>>double *ptr[30][6];

>>For me, it is an pointer array with 180 pointers in it.
>>I was told that * means it is an array of y length.
>>Does it mean taht *ptr[30][6] can be ptr[y][30][6] or
ptr
>>[30][6][y]?
>>If it can represent a 3D array, how can I allocate
>>memeory to *ptr[30][6] and make it to ptr[y][30][6]?

>.

Thu, 22 Sep 2005 07:17:20 GMT

 Page 1 of 1 [ 5 post ]

Relevant Pages