459,290 Members | 1,671 Online
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

# Pointer to qualified poitner to qualified object

 P: n/a 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 Jun 27 '08 #1
14 Replies

 P: n/a 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 #define DIM_1 2 #define DIM_2 3 #define DIM_3 4 typedef double tmp_tensor3[DIM_2][DIM_3]; void func(tmp_tensor3 *d3array); int main(void) { double array[DIM_1][DIM_2][DIM_3]; int c1, c2, c3; for (c1 = 0; c1 != DIM_1; ++c1) for (c2 = 0; c2 != DIM_2; ++c2) for (c3 = 0; c3 != DIM_3; ++c3) { array[c1][c2][c3] = c1 + c2 + c3 + 0.5; } func(array); return 0; } void func(tmp_tensor3 *d3array) { int c1, c2, c3; for (c1 = 0; c1 != DIM_1; ++c1) { for (c2 = 0; c2 != DIM_2; ++c2){ for (c3 = 0; c3 != DIM_3; ++c3) { printf("%f ", d3array[c1][c2][c3]); } putchar('\n'); } putchar('\n'); } putchar('\n'); } /* END new.c */ -- pete Jun 27 '08 #2

 P: n/a On Fri, May 30, 2008 at 03:13:09AM -0500, pete wrote: Szabolcs Borsanyi wrote: >Deal all,The typetypedef double ***tmp_tensor3;is meant to represent a three-dimensional array. For some reasons thestandard array-of-array-of-array will not work in my case. /* BEGIN new.c */ #include #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 inmy 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 Jun 27 '08 #3

 P: n/a 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 Jun 27 '08 #4

 P: n/a 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, 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 Jun 27 '08 #5

 P: n/a Flash Gordon 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 constantpointer, you can perform the cast safely. This applies to each layerof indirection. For example, 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) ks***@mib.org Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Jun 27 '08 #6

 P: n/a On Fri, 30 May 2008 07:31:54 UTC, Szabolcs Borsanyi

 P: n/a "Herbert Rosenau" Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Jun 27 '08 #8

 P: n/a On Mon, 2 Jun 2008 20:35:24 UTC, Keith Thompson

 P: n/a "Herbert Rosenau" "Herbert Rosenau" Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Jun 27 '08 #10

 P: n/a On May 30, 7:31 pm, Szabolcs Borsanyi 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). Jun 27 '08 #11

 P: n/a On Wed, 4 Jun 2008 21:00:02 UTC, Keith Thompson

 P: n/a "Herbert Rosenau" "Herbert Rosenau" Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Jun 27 '08 #13

 P: n/a On Thu, 5 Jun 2008 21:22:52 UTC, Keith Thompson

 P: n/a "Herbert Rosenau" Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Jun 27 '08 #15

### This discussion thread is closed

Replies have been disabled for this discussion.