指向合格对象的合格poitner的指针 [英] Pointer to qualified poitner to qualified object

查看:70
本文介绍了指向合格对象的合格poitner的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部交易,


类型


typedef double *** tmp_tensor3;


表示三维阵列。由于某些原因,

标准的数组数组在我的情况下不起作用。


我可以将这种类型的对象转换为以下类型?


typedef doule * const * const * tensor3;


这表明我不想搞砸指数,只需

修改数字。


假设我有一个tensor3类型的变量,并且函数不是为了
修改数字,以便期望一个const_testor3对象;


typedef const doule * const * const * const_tensor3;


但是这也是没有演员就无法工作。


有没有一种很好的方式来表达一个不应该的函数的含义

来改变张量内的数字?比如将所有这些放在一个结构中

或更复杂的东西?

Szabolcs

解决方案

Szabolcs Borsanyi写道:


全部交易,


类型


typedef double *** tmp_tensor3;


用于表示三维数组。出于某些原因,

标准的数组数组在我的情况下不起作用。



/ * 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(无效)

{

双数组[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){

数组[c1] [c2] [c3] = c1 + c2 + c3 + 0.5;

}

func(数组);

返回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


2008年5月30日星期五上午03:13:09 -0500,pete写道:
< blockquote class =post_quotes>
Szabolcs Borsanyi写道:


>全部交易,

类型

typedef double *** tmp_tensor3;

意味着代表一个三维数组。出于某些原因,
标准数组数组在我的情况下不起作用。



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



我很遗憾浪费你的时间,但我觉得有一个误解。

我知道如何使用多维数组和还有怎么玩

双*** - 像表示。我的问题提到了关于将(双***)指针转换为合格版本的C标准'

意见。


我的声明是


>由于某些原因,标准的数组数组在我的情况下不起作用。



并不意味着我正在努力解决它,而是我确实知道

双倍[] [ ] []代表了一个与我想象的不同的对象。

实际上我的三维数组有一些相同的元素。

(具有周期性边界条件的3d格子前两个指数)。


所以问题仍然存在:如何合法地从(双***)转换为

(double * const * const *)然后再到(double const * const * const *)。

我担心类型的兼容性。


Szabolcs


Szabolcs Borsanyi写道:


所以问题仍然存在:如何合法转换(双***)

(double * const * const *)然后再到(double const * const * const *)。

我担心兼容性类型。



由于你是从非常量指针转换为常量指针,

你可以安全地执行转换。这适用于

间接的每一层。例如,


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; //数据立方体的维度

int i,j,k; //迭代的变量

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
const double el = col [k];

}

}

}


我相信满足您的问题。


-

Andrew Kerr


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

解决方案

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


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


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


这篇关于指向合格对象的合格poitner的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆