多维数组作为一维数组 [英] multi dimensional arrays as one dimension array

查看:77
本文介绍了多维数组作为一维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主题可能会产生误导。

无论如何,此代码是否有效:


#include< stdio.h>


void f(double * p,size_t size){while(size--)printf("%f\ n",* p ++); }

int main(void){

double array [2] [1] = {{3.14},{42.6}};

f ((double *)数组,sizeof数组/ sizeof **数组);

返回0;

}


假设double [2] [1] to double *是实现定义

或未定义的行为,用(void *)替换cast。


因为数组不是允许在

元素之间使用填充字节,这似乎对我有用。

The subject might be misleading.
Regardless, is this code valid:

#include <stdio.h>

void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); }
int main(void) {
double array[2][1] = { { 3.14 }, { 42.6 } };
f((double *)array, sizeof array / sizeof **array);
return 0;
}

Assuming casting double [2][1] to double * is implementation defined
or undefined behavior, replace the cast with (void *).

Since arrays are not allowed to have padding bytes in between of
elements, it seems valid to me.

推荐答案

vi ****** @ gmail.com 写道:

主题可能会产生误导。

无论如何,这段代码是否有效:


#include< stdio.h>


void f(double * p,size_t size){while(size--)printf("%f \ n",* p ++); }

int main(void){

double array [2] [1] = {{3.14},{42.6}};

f ((double *)数组,sizeof数组/ sizeof **数组);

返回0;

}


假设double [2] [1] to double *是实现定义

或未定义的行为,用(void *)替换cast。


因为数组不是允许在

元素之间有填充字节,这似乎对我有用。
The subject might be misleading.
Regardless, is this code valid:

#include <stdio.h>

void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); }
int main(void) {
double array[2][1] = { { 3.14 }, { 42.6 } };
f((double *)array, sizeof array / sizeof **array);
return 0;
}

Assuming casting double [2][1] to double * is implementation defined
or undefined behavior, replace the cast with (void *).

Since arrays are not allowed to have padding bytes in between of
elements, it seems valid to me.



逐步浏览一维数组

并通过连续的一维数组开启

仅定义为字符类型的元素

并且只因为

任何对象都可以被视为字符类型的数组。


-

pete

Stepping through a one dimensional array
and on through a consecutive one dimensional array
is only defined for elements of character type
and only because
any object can be treated as an array of character type.

--
pete


8月29日上午6:39,pete< pfil ... @ mindspring.comwrote:
On Aug 29, 6:39 am, pete <pfil...@mindspring.comwrote:

vipps ... @ gmail.com写道:
vipps...@gmail.com wrote:

该主题可能会产生误导。

无论如何,此代码是否有效:
The subject might be misleading.
Regardless, is this code valid:


#include< stdio.h>
#include <stdio.h>


void f(double * p,size_t size){while(size--)printf("%f\\\
",* p ++ ); }

int main(void){

double array [2] [1] = {{3.14},{42.6}};

f ((double *)数组,sizeof数组/ sizeof **数组);

返回0;

}
void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); }
int main(void) {
double array[2][1] = { { 3.14 }, { 42.6 } };
f((double *)array, sizeof array / sizeof **array);
return 0;
}


假设将double [2] [1]转换为double *是实现定义的

或未定义的行为,请将转换替换为(void *)。
Assuming casting double [2][1] to double * is implementation defined
or undefined behavior, replace the cast with (void *).


由于不允许数组在

元素之间有填充字节,所以对我来说似乎是有效的。
Since arrays are not allowed to have padding bytes in between of
elements, it seems valid to me.



逐步通过一维数组

并通过连续的一维数组开启

仅定义为字符类型的元素

并且只因为

任何对象都可以被视为字符类型的数组。


Stepping through a one dimensional array
and on through a consecutive one dimensional array
is only defined for elements of character type
and only because
any object can be treated as an array of character type.



据我所知,你说我的代码调用未定义的

行为。

在哪个假设中(或者真的,如果确实存在这样的实现)

实现我的代码不起作用,为什么?

So as I understand it you are saying my code invokes undefined
behavior.
In which hypothetical (or real, if such implementation does exist)
implementation my code won''t work, and why?


vi ****** @ gmail.com 写道:

8月29日,6:39 am,pete< pfil ... @ mindspring.comwrote:
On Aug 29, 6:39 am, pete <pfil...@mindspring.comwrote:

> vipps ... @ gmail.com写道:
>vipps...@gmail.com wrote:

>>主题可能会产生误导。
无论如何,此代码是否有效:
#include< stdio.h>
void f(double * p,size_t size){while(size--)printf("%f\ n",* p ++); }
int main(void){
double array [2] [1] = {{3.14},{42.6}};
f((double *)array,sizeof array / sizeof ** array);
返回0;
}
假设将double [2] [1]转换为double *是实现定义的
或未定义的行为,请将转换为(void) *)。
由于不允许数组在
元素之间有填充字节,所以对我来说似乎是有效的。
>>The subject might be misleading.
Regardless, is this code valid:
#include <stdio.h>
void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); }
int main(void) {
double array[2][1] = { { 3.14 }, { 42.6 } };
f((double *)array, sizeof array / sizeof **array);
return 0;
}
Assuming casting double [2][1] to double * is implementation defined
or undefined behavior, replace the cast with (void *).
Since arrays are not allowed to have padding bytes in between of
elements, it seems valid to me.


逐步浏览一维数组
并通过连续的一维数组
仅为字符类型的元素定义
并且只是因为
任何对象都可以视为字符类型的数组。

Stepping through a one dimensional array
and on through a consecutive one dimensional array
is only defined for elements of character type
and only because
any object can be treated as an array of character type.



据我所知,你说我的代码调用了未定义的

行为。

在哪个假设中(或真实的,如果确实存在这样的实现)

实现我的代码不会工作,为什么?


So as I understand it you are saying my code invokes undefined
behavior.
In which hypothetical (or real, if such implementation does exist)
implementation my code won''t work, and why?



我不知道。

我认为设计系统会更困难

它不会工作,而不是设计一个。


认为它被允许不工作。


-

pete

I don''t know.
I think it would be more difficult to design a system
where it wouldn''t work, than to design one where it would.

Think of it as being allowed not to work.

--
pete


这篇关于多维数组作为一维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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