指针转换 [英] Pointer conversions

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

问题描述

以下是否保证始终以* C90 *代码工作?


1.


#include< stdio.h>

void some_func(int * p,const size_t SIZE)

{

size_t i;


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

printf("%d",p [i]);

}

int main(无效)

{

int array [10] [5] = {0};


some_func(array [0],sizeof(array)/ sizeof(** array));


puts("");


返回0;

}


以上打印50个零。我认为它可以保证工作,因为所有

数组都是它们元素的序列。


2.


#include< stdio.h>

#include< stdlib.h>

int main(无效)

{

size_t i,j;


int array [50] = {0};


int(* p)[ 5] =(int(*)[5])(& array [0]);


for(i = 0; i< 10; ++ i)
for(j = 0; j< 5; ++ j)

printf("%d",p [i] [j]);


put("");


返回0;

}

这里p表现作为二维矩阵,即10x5矩阵。我认为

它保证与第一个原因相同,即

我们可以将整数的数组(序列)视为各种类型的整数

数组。

Are the following guaranteed to work always as *C90* code?

1.

#include <stdio.h>
void some_func(int *p, const size_t SIZE)
{
size_t i;

for(i=0; i<SIZE; ++i)
printf("%d ", p[i]);
}
int main(void)
{
int array[10][5]= {0};

some_func(array[0], sizeof(array)/sizeof(**array));

puts("");

return 0;
}

The above prints 50 zeros. I think it is guaranteed to work, since all
arrays are sequences of their elements.

2.

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
size_t i, j;

int array[50]= {0};

int (*p)[5]= (int (*)[5])(&array[0]);

for (i= 0; i< 10; ++i)
for(j=0; j<5; ++j)
printf("%d ", p[i][j]);

puts("");

return 0;
}
Here p behaves as a 2-dimensional matrix, that is a 10x5 matrix. I think
it is guaranteed to work for the same reason as the first one, that is
we can treat an array (sequence) of integers as various types of integer
arrays.

推荐答案

Ioannis Vranos写道:
Ioannis Vranos wrote:

>

以下保证始终作为* C90 *代码工作吗?


1.


# include< stdio.h>


void some_func(int * p,const size_t SIZE)

{

size_t i;


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

printf("%d",p [i]); < br $>
}


int main(无效)

{

int array [10] [5] = {0};


some_func(array [0],sizeof(array)/ sizeof(** array));


puts (");


返回0;

}


以上打印50个零。我认为它可以保证工作,因为所有

数组都是它们元素的序列。
>
Are the following guaranteed to work always as *C90* code?

1.

#include <stdio.h>

void some_func(int *p, const size_t SIZE)
{
size_t i;

for(i=0; i<SIZE; ++i)
printf("%d ", p[i]);
}

int main(void)
{
int array[10][5]= {0};

some_func(array[0], sizeof(array)/sizeof(**array));

puts("");

return 0;
}

The above prints 50 zeros. I think it is guaranteed to work, since all
arrays are sequences of their elements.



不能保证工作。

问题是没有int $ / $
的数组超过5个成员在任何地方声明。


指向char的指针允许逐步执行任何对象的字节

,但这是通过特殊规则。


我无法想象你的代码可能失败的任何机制,

但是你所说的保证不存在。


指向int的指针,

结束了运行5 int数组的边界。


-

pete

It is not guaranteed to work.
The problem is that there is no array of int
with more than 5 members declared anywhere.

Pointers to char are allowed to step through the bytes
of any object, but that''s by a special rule.

I can''t conceive of any mechanism by which your code could fail,
but the guarantee of which you speak, is not there.

Your pointer to int,
is over running the boundaries of an array of 5 int.

--
pete


Ioannis Vranos写道:
Ioannis Vranos wrote:

以下保证可以使用总是* C90 *代码?
Are the following guaranteed to work always as *C90* code?



[处理一个int数组[10] [5],因为它是int数组[1] [50]]

那个''标准定义没有行为的那些东西之一,没有任何理由不定义一个行为的b $ b。如果它失败了,我会非常惊讶,但这不会使实现不符合要求。除非

有一些实现失败,我不反对它

被允许。

(另一个这样的事情是结构破解,或用%x打印一个非负的签名

int。它们可能会失败,但我看不出它们可能的任何方式,

除了实施*故意*敌对。)


-

Army1987(将NOSPAM替换为电子邮件)

[ Treating an int array[10][5] as it were int array[1][50] ]
That''s one of those things for which the standard defines no behavior,
without any reason not to do define one. I''d be very surprised if it
failed, but that would not make an implementation non-conforming. Unless
there is some implementation on which that fails, I''d not object to it
being allowed.
(Another such thing is the struct hack, or printing a nonnegative signed
int with %x. They can fail, but I can''t see any way in which they could,
other than the implementation being *intentionally* hostile.)

--
Army1987 (Replace "NOSPAM" with "email")


On Sun,2008年1月6日13:53:56 +0000,Army1987写道:
On Sun, 06 Jan 2008 13:53:56 +0000, Army1987 wrote:

Ioannis Vranos写道:
Ioannis Vranos wrote:

>以下是否保证始终作为* C90 *代码使用?
>Are the following guaranteed to work always as *C90* code?



[处理一个int数组[10] [5]因为它是int数组[1] [50]]那就是一个
$ b标准定义没有行为的那些东西的$ b,没有任何

理由不定义一个。如果它失败了我会非常惊讶,但是

不会使实现不符合要求。除非有一些实现失败,否则我不会反对它允许使用

[ Treating an int array[10][5] as it were int array[1][50] ] That''s one
of those things for which the standard defines no behavior, without any
reason not to do define one. I''d be very surprised if it failed, but
that would not make an implementation non-conforming. Unless there is
some implementation on which that fails, I''d not object to it being
allowed.



我记得,它确实在一些实现上失败了,因为优化器

并没有意识到p1是可能的[10] [3]和p2 [5] [8]重叠(并且

实际上它在标准C中是不可能的),所以代码如

p1 [10] [3] = 5;

p2 [5] [8] = 3;

返回p1 [10] [3];


将返回5而不重新读取p1 [10] [3]。但是,我现在不能用一个具体的例子来显示行为。这是一个GCC的b $ b版本,但我现在使用的那个似乎处理数组

访问的方式不同。

As I recall, it does fail on some implementations, because the optimiser
doesn''t realise it''s possible for p1[10][3] and p2[5][8] to overlap (and
in fact it isn''t possible in standard C), so code such as

p1[10][3] = 5;
p2[5][8] = 3;
return p1[10][3];

would return 5 without re-reading p1[10][3]. However, I cannot come up
with a concrete example showing the behaviour right now. It was with one
version of GCC, but the one I''m using now appears to handle array
accesses differently.


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

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