Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Pointer to qualified poitner to qualified object

Reply
Thread Tools

Pointer to qualified poitner to qualified object

 
 
Szabolcs Borsanyi
Guest
Posts: n/a
 
      05-30-2008
Deal all,

The type

typedef double ***tmp_tensor3;

is meant to represent a three-dimensional array. For some reasons the
standard array-of-array-of-array will not work in my case.

Can I convert an object of this type to the following type?

typedef doule * const * const * tensor3;

This would indicate that I would not like to mess up the indices, just
modify the numbers.

Suppose I have a variable of type tensor3, and a function is not meant to
modify the numbers, so that would expect a const_testor3 object;

typedef const doule * const * const * const_tensor3;

But that also will not work without a cast.

Is there a good way to express the meaning of a function that is not supposed
to change the numbers inside of the tensor? Like putting all that in a struct
or something more sophisticated?

Szabolcs
 
Reply With Quote
 
 
 
 
Szabolcs Borsanyi
Guest
Posts: n/a
 
      05-30-2008
On Fri, May 30, 2008 at 03:13:09AM -0500, pete wrote:
> Szabolcs Borsanyi wrote:
>> Deal all,
>>
>> The type
>>
>> typedef double ***tmp_tensor3;
>>
>> is meant to represent a three-dimensional array. For some reasons the
>> standard array-of-array-of-array will not work in my case.

>
> /* BEGIN new.c */
>
> #include <stdio.h>
>
> #define DIM_1 2
> #define DIM_2 3
> #define DIM_3 4
>
> typedef double tmp_tensor3[DIM_2][DIM_3];


I am very sorry for wasting your time, but I think there is a misunderstanding.
I do know how to use multidimensional arrays and also how to play with
double ***-like representations. My question referred to the C standard's
opinion about converting a (double ***) pointer to qualified versions.

My statement that
>> For some reasons the standard array-of-array-of-array will not work in
>> my case.

had not the meaning that I am struggled with it, but rather, I do know
that double[][][] represents a different object than what I have in mind.
In fact my three dimensional array have some identical elements.
(3d lattice with periodic boundary conditions in the first two indices).

So the question is still there: how to legally convert from (double***) to
(double * const * const *) and then that to (double const * const * const *).
I am worried about the compatibility of the types.

Szabolcs

 
Reply With Quote
 
 
 
 
Andrew Kerr
Guest
Posts: n/a
 
      05-30-2008
Szabolcs Borsanyi wrote:
> So the question is still there: how to legally convert from (double***) to
> (double * const * const *) and then that to (double const * const * const *).
> I am worried about the compatibility of the types.


Since you are casting from a non-constant pointer to a constant pointer,
you can perform the cast safely. This applies to each layer of
indirection. For example,

typedef double * const * const * tensor3;
typedef const double * const * const * const_tensor3;

double ***ppp_tensor;
tensor3 T = (tensor3)ppp_tensor;
const_tensor3 cT = (const_tensor3)T;

const int M, N, K; // dimensions of data cube
int i, j, k; // variables of iteration
for (i = 0; i < M; i++) {
double * const * const row = T[i];
for (j = 0; j < N; j++) {
double * const col = row[j];
for (k = 0; k < K; k++) {
double el = col[k];
}
}
}

for (i = 0; i < M; i++) {
const double * const * const row = cT[i];
for (j = 0; j < N; j++) {
const double * const col = row[j];
for (k = 0; k < K; k++) {
const double el = col[k];
}
}
}

I believe that satisfies your questions.

--
Andrew Kerr
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      05-30-2008
Andrew Kerr wrote, On 30/05/08 19:03:
> Szabolcs Borsanyi wrote:
>> So the question is still there: how to legally convert from
>> (double***) to
>> (double * const * const *) and then that to (double const * const *
>> const *).
>> I am worried about the compatibility of the types.

>
> Since you are casting from a non-constant pointer to a constant pointer,
> you can perform the cast safely. This applies to each layer of
> indirection. For example,


<snip>

There is a good discussion of the issues and the reason for the
conversion not being explicit in question 11.10 of the comp.lang.c FAQ
at http://c-faq.com/
--
Flash Gordon
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-30-2008
Flash Gordon <(E-Mail Removed)> writes:
> Andrew Kerr wrote, On 30/05/08 19:03:
>> Szabolcs Borsanyi wrote:
>>> So the question is still there: how to legally convert from
>>> (double***) to
>>> (double * const * const *) and then that to (double const * const *
>>> const *).
>>> I am worried about the compatibility of the types.

>> Since you are casting from a non-constant pointer to a constant
>> pointer, you can perform the cast safely. This applies to each layer
>> of indirection. For example,

>
> <snip>
>
> There is a good discussion of the issues and the reason for the
> conversion not being explicit in question 11.10 of the comp.lang.c FAQ
> at http://c-faq.com/


I think you mean "not being implicit".

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Herbert Rosenau
Guest
Posts: n/a
 
      06-02-2008
On Fri, 30 May 2008 07:31:54 UTC, Szabolcs Borsanyi
<(E-Mail Removed)> wrote:

> Deal all,
>
> The type
>
> typedef double ***tmp_tensor3;
>
> is meant to represent a three-dimensional array. For some reasons the
> standard array-of-array-of-array will not work in my case.


No, it means simply a pointer to a pointer to a pointer to a double.

double a[2] is not a pointer but an array of 2 doubles
double a[2][2] is an array of 2 arrrays of 2 doubles
double a[2][2][2] means an array of an array of an array of 2 doubles.


An array is not a pointer and a pointer is not an array.

double *p is a pointer to a double or a ppointer to an array of
unspezified size of doubles.

double **p is a pointer to a pointer to a double or
a pointer to a pointer to an array of unspezified size of type double
or
a pointer to an array of unspezified size of pointers to a double

C is a bit lazy as it does not spezifies that a pointer points to a
single object or an array of objects of that type.

So again: a pointer is not an array.
An array is not a pointer.
The address of an array may decay to a pointer but it is not an array.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2R Deutsch ist da!
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-02-2008
"Herbert Rosenau" <(E-Mail Removed)> writes:
[...]
> C is a bit lazy as it does not spezifies that a pointer points to a
> single object or an array of objects of that type.

[...]

Incorrect. A pointer to a single object, such as
int *ptr1;
and a pointer to an array, such as:
int (*ptr2)[2];
are of two distinct types.

The valid point I suspect you were making is that an array is
typically accessed via a pointer to its first element, with pointer
arithmetic leting you step through the elements of the array.
Pointers to arrays actually aren't used very often (except implicitly
in operations on declared multi-dimensional arrays).

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Herbert Rosenau
Guest
Posts: n/a
 
      06-04-2008
On Mon, 2 Jun 2008 20:35:24 UTC, Keith Thompson <(E-Mail Removed)> wrote:

> "Herbert Rosenau" <(E-Mail Removed)> writes:
> [...]
> > C is a bit lazy as it does not spezifies that a pointer points to a
> > single object or an array of objects of that type.

> [...]
>
> Incorrect. A pointer to a single object, such as
> int *ptr1;
> and a pointer to an array, such as:
> int (*ptr2)[2];
> are of two distinct types.
>
> The valid point I suspect you were making is that an array is
> typically accessed via a pointer to its first element, with pointer
> arithmetic leting you step through the elements of the array.
> Pointers to arrays actually aren't used very often (except implicitly
> in operations on declared multi-dimensional arrays).
>

Really? In my programs I make heavy use of arrays to define define
data - but use only pointers to access the data therein.

The usage of multidimensional arrays is in contrast to that really
rare.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2R Deutsch ist da!
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-04-2008
"Herbert Rosenau" <(E-Mail Removed)> writes:
> On Mon, 2 Jun 2008 20:35:24 UTC, Keith Thompson <(E-Mail Removed)> wrote:
>> "Herbert Rosenau" <(E-Mail Removed)> writes:
>> [...]
>> > C is a bit lazy as it does not spezifies that a pointer points to a
>> > single object or an array of objects of that type.

>> [...]
>>
>> Incorrect. A pointer to a single object, such as
>> int *ptr1;
>> and a pointer to an array, such as:
>> int (*ptr2)[2];
>> are of two distinct types.
>>
>> The valid point I suspect you were making is that an array is
>> typically accessed via a pointer to its first element, with pointer
>> arithmetic leting you step through the elements of the array.
>> Pointers to arrays actually aren't used very often (except implicitly
>> in operations on declared multi-dimensional arrays).
>>

> Really?


Yes. I may not have stated it as clearly as I'd like. Actual
multidimensional arrays (i.e., arrays of arrays) are probably fairly
rare, but I suspect they're the most common context in which pointers
to arrays are used.

> In my programs I make heavy use of arrays to define define
> data - but use only pointers to access the data therein.


Right, but presumably you use a pointer to the array's *element type*,
not to the array.

> The usage of multidimensional arrays is in contrast to that really
> rare.


Agreed.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      06-05-2008
On May 30, 7:31 pm, Szabolcs Borsanyi <(E-Mail Removed)> wrote:
>
> typedef double ***tmp_tensor3;
>
> Can I convert an object of this type to the following type?
>
> typedef doule * const * const * tensor3;


Yes, but you must use a cast. (IMHO this is
a defect in the language - other C-like
languages allow the conversion without a cast).
 
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
Odd behavior of object equality/identity in the context of relativevs fully qualified imports Nathan Rice Python 0 12-15-2011 02:34 PM
modifying const qualified object indirectly gibby C Programming 6 04-18-2009 02:37 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
Rationale behind requiring ampersand and fully qualified name to form a member function pointer Alexander Grigoriev C++ 0 09-12-2003 03:18 AM



Advertisments