数组初始值设定项/转换为指针 [英] array initializer / conversion to pointer

查看:58
本文介绍了数组初始值设定项/转换为指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我有一个非常简单的问题。我希望有类似的东西


int

main()

{

double array [ 3] [3] = {{1,2,3},{4,5,6},{7,8,9}};

double ** field;

field = array;

}


编译器在作业中抱怨不允许转换

field = array;

该字段的类型为double **是必要的。如何管理如上所述的

初始化?


问候,

alex

Hello,

I''ve got a quite simple question. I want to have something like

int
main()
{
double array[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
double **field;
field = array;
}

The compiler complains an not allowed conversion in the assignment
field = array;
It''s neccessary that field is of type double**. How to manage an
initialization like the above?

regards,
alex

推荐答案

Alexander Stippler< st ** @ mathematik.uni-ulm.de>潦草地写道:
Alexander Stippler <st**@mathematik.uni-ulm.de> scribbled the following:
你好,
我有一个非常简单的问题。我希望有一些像
int
main()
{/ / double array [3] [3] = {{1,2,3},{4,5, 6},{7,8,9}};
双**字段;
字段=数组;
}
编译器在作业中抱怨不允许转换
field = array;
字段是double **类型是必要的。如何管理如上所述的
初始化?
Hello, I''ve got a quite simple question. I want to have something like int
main()
{
double array[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
double **field;
field = array;
} The compiler complains an not allowed conversion in the assignment
field = array;
It''s neccessary that field is of type double**. How to manage an
initialization like the above?




将字段声明为double(* field)[3]。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰--- ----- \

\ - http: //www.helsinki.fi/~palaste ---------------------规则! -------- /

更强,没有。黑暗面更具诱惑力,狡猾,更脆弱。

- Mika P. Nieminen



Declare field as double (*field)[3] instead.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Stronger, no. More seductive, cunning, crunchier the Dark Side is."
- Mika P. Nieminen





Alexander Stippler写道:


Alexander Stippler wrote:
你好,

我有一个非常简单的问题。我希望有类似的东西

主要的()
{/ />双数组[3] [3] = {{1,2,3},{ 4,5,6},{7,8,9}};
双**字段;
字段=数组;
}

编译器抱怨不允许在作业中进行转换
field = array;
该字段的类型为double **是必要的。如何管理如上所述的
初始化?
Hello,

I''ve got a quite simple question. I want to have something like

int
main()
{
double array[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
double **field;
field = array;
}

The compiler complains an not allowed conversion in the assignment
field = array;
It''s neccessary that field is of type double**. How to manage an
initialization like the above?




这是常见问题解答。阅读常见问题6.18位于:
http ://www.eskimo.com/~scs/C-faq/q6.18.html


正如常见问题所示,请这样做:

double(* field)[3] = array;

下面使用函数PrintARRAY显示一个示例,使用

typedef。


如果,正如你所说的那样,字段必须是双**类型,那么

你仍然可以以访问元素为代价来实现它

a稍微困难一些。这在下面的PrintARRAY2和

PrintARRAY3函数中显示。


#include< stdio.h>


#define ROW 3

#define COL 4

typedef double(* ARR)[COL];


void PrintARRAY(ARR row);

void PrintARRAY2(double ** field);

void PrintARRAY3(double ** field);


int main(void)

{

double array [ROW] [COL] = {{1,2,3,11},{4,5, 6,22},{7,8,9,33}};

ARR this = array;

double ** field =(double **)array;


puts(使用函数PrintARRAY);

PrintARRAY(this);

puts(" \ nUsing function) printARRAY2");

PrintARRAY2(字段);

puts(" \ nUsing function printARRAY3");

PrintARRAY3(field);

返回0;

}


无效PrintARRAY(ARR行)

{

int i,j;


for(i = 0; i< ROW; i ++)

{

for(j = 0; j< COL; j ++)

printf("%。2f",row [i] [j]);

putchar(''\ n'');

}

返回;

}


无效PrintARRAY2(双**字段)

{

int i,j;

double(* arr)[COL] =(double(*)[COL])字段;


for(i = 0; i< ROW; i ++,arr ++)

{

for(j = 0; j< COL ; j ++)

printf("%。2f",(* arr)[j]);

putchar(''\ n'');

}

返回;

}


无效PrintARRAY3(双**字段)

{

int i,j;

double * arr =(double *)field;


for (i = 0; i< ROW; i ++)

{

for(j = 0; j< COL; j ++)

printf("%。2f",arr [i * COL + j]);

putchar(''\ n'');

}

返回;

}

-

Al Bowers

Tampa,Fl USA

mailto: xa * *****@myrapidsys.com (删除x以发送电子邮件)
http://www.geocities.com/abowers822/



This is a faq question and answer. Read faq question 6.18 located:
http://www.eskimo.com/~scs/C-faq/q6.18.html

As the faq suggests, make this:
double (*field)[3] = array;
An example is shown below using in function PrintARRAY, using a
typedef.

If, as you say, it is neccessary that field be type double**, then
you can still do it at the expense of making access to the elements
a little more difficult. This is shown in functions PrintARRAY2 and
PrintARRAY3 below.

#include <stdio.h>

#define ROW 3
#define COL 4

typedef double (*ARR)[COL];

void PrintARRAY(ARR row);
void PrintARRAY2(double **field);
void PrintARRAY3(double **field);

int main(void)
{
double array[ROW][COL] = {{1,2,3,11}, {4,5,6,22}, {7,8,9,33}};
ARR this = array;
double **field = (double **)array;

puts("Using function PrintARRAY");
PrintARRAY(this);
puts("\nUsing function printARRAY2");
PrintARRAY2(field);
puts("\nUsing function printARRAY3");
PrintARRAY3(field);
return 0;
}

void PrintARRAY(ARR row)
{
int i,j;

for(i = 0; i < ROW;i++)
{
for(j = 0; j < COL; j++)
printf("%.2f ",row[i][j]);
putchar(''\n'');
}
return;
}

void PrintARRAY2(double **field)
{
int i,j;
double (*arr)[COL] = (double (*)[COL])field;

for(i = 0; i < ROW;i++,arr++)
{
for(j = 0; j < COL; j++)
printf("%.2f ",(*arr)[j]);
putchar(''\n'');
}
return;
}

void PrintARRAY3(double **field)
{
int i,j;
double *arr= (double *)field;

for(i = 0;i < ROW; i++)
{
for(j = 0; j < COL;j++)
printf("%.2f ",arr[i*COL+j]);
putchar(''\n'');
}
return;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


Joona I Palaste写道:
Joona I Palaste wrote:
Alexander Stippler< st ** @ mathematik.uni-ulm.de>潦草地写道:
Alexander Stippler <st**@mathematik.uni-ulm.de> scribbled the following:
你好,


我有一个非常简单的问题。我希望有类似的东西
I''ve got a quite simple question. I want to have something like


int
main()
{
double array [3] [3] = {{ 1,2,3},{4,5,6},{7,8,9}};
double ** field;
field = array;
}
int
main()
{
double array[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
double **field;
field = array;
}


编译器在作业中抱怨不允许转换
field = array;
该字段的类型为double **是必要的。如何管理如上所述的
初始化?
The compiler complains an not allowed conversion in the assignment
field = array;
It''s neccessary that field is of type double**. How to manage an
initialization like the above?



将字段声明为double(* field)[3]而不是。



Declare field as double (*field)[3] instead.




对不起,但我不能影响字段的类型,因为我已经在

中提到了我的第一个帖子!它对我的影响是外在的。我需要它是

双**,那又怎么样?


最好的问候,alex



Sorry, but I cannot influence the type of field, as I already mentioned in
my first posting! It is ''external'' to my influence. I need it to be
double **, so what?

best regards, alex


这篇关于数组初始值设定项/转换为指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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