PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Re: Pointer to qualified poitner to qualified object
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: Pointer to qualified poitner to qualified object

Réponse
 
LinkBack Outils de la discussion
Vieux 30/05/2008, 09h13   #1
pete
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pointer to qualified poitner to qualified object

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];

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
  Réponse avec citation
Vieux 30/05/2008, 19h03   #2
Andrew Kerr
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pointer to qualified poitner to qualified object

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
  Réponse avec citation
Vieux 30/05/2008, 20h46   #3
Flash Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pointer to qualified poitner to qualified object

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
  Réponse avec citation
Vieux 30/05/2008, 23h38   #4
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pointer to qualified poitner to qualified object

Flash Gordon <spam@flash-gordon.me.uk> 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) kst-u@mib.org <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"
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h36.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12746 seconds with 12 queries