通过指向2d数组的指针访问。 [英] Accessing via a pointer to 2d array.
问题描述
大家好,
我有一个2d浮点数组,因此我已经声明了一个指针:
float Matrix [4] [3] = {/ * snipped initialisation * /};
float(* StoredMatrix)[4] [3];
为了在程序中创建副本"注册表"我自己设计的。
我按如下方式复制Marix数组:
StoredMatrix = malloc(sizeof Matrix);
if(!StoredMatrix)
{
memcpy((void *)StoredMatrix,(void *)Matrix,sizeof Matrix);
}
其他
{/ *采取行动* /}
我的问题是这是否合法,如果是是否有危险?
到目前为止,复制的版本使用[]运算符取消引用OK - 例如
使用* StoredMatrix [2] [1]等。我'我找不到K& R对这个主题的帮助。
任何建议都赞赏。
TheS
Hi all,
I have a 2d float array to which I have declared a pointer thus:
float Matrix[4][3] = {/* snipped initialisation */ };
float (*StoredMatrix)[4][3];
So as to create a copy in a program "registry" of my own devising.
I copy the Marix array as follows:
StoredMatrix = malloc(sizeof Matrix);
if(!StoredMatrix)
{
memcpy((void*)StoredMatrix, (void*)Matrix, sizeof Matrix);
}
else
{ /* take action */ }
My question is whether this is legal, and if so whether it is dangerous?
So far, the copied version dereferences OK using the [] operators - eg
using *StoredMatrix[2][1] etc. I''m not finding K&R helpful on the subject.
Any advice appreciated.
TheS
推荐答案
Thes< no *@ctually.myaddress.com>潦草地写道:
Thes <no*@ctually.myaddress.com> scribbled the following:
大家好,
我有一个2d浮点数组,我已经声明了一个指针:
float Matrix [4] [3] = {/ * snipped initialisation * /};
float(* StoredMatrix)[4] [3];
为了在程序注册表中创建副本我自己设计的。
我按如下方式复制Marix数组:
StoredMatrix = malloc(sizeof Matrix);
if(!StoredMatrix)
当然你的意思是( StoredMatrix),或者更好,
if(StoredMatrix!= NULL)在这里?你在这里写的方式是问
的麻烦。
{
memcpy((void *)StoredMatrix,(void *)Matrix,sizeof Matrix );
}
其他
{/ *采取行动* /}
我的问题是这是否合法,若然是危险?
到目前为止,复制版本使用[]运算符取消引用OK - 例如
使用* StoredMatrix [2] [1]等。我找不到K& R对该主题有帮助。
Hi all, I have a 2d float array to which I have declared a pointer thus: float Matrix[4][3] = {/* snipped initialisation */ };
float (*StoredMatrix)[4][3];
So as to create a copy in a program "registry" of my own devising. I copy the Marix array as follows: StoredMatrix = malloc(sizeof Matrix);
if(!StoredMatrix)
Surely you mean if (StoredMatrix), or better yet,
if (StoredMatrix != NULL) here? The way you''ve written here is asking
for trouble.
{
memcpy((void*)StoredMatrix, (void*)Matrix, sizeof Matrix);
}
else
{ /* take action */ } My question is whether this is legal, and if so whether it is dangerous?
So far, the copied version dereferences OK using the [] operators - eg
using *StoredMatrix[2][1] etc. I''m not finding K&R helpful on the subject.
纠正这个明显的错误,我没有看到任何问题。数组是
保证是连续的,所以你没有对齐或偏移
问题。
-
/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰-------- \
\ - http://www.helsinki.fi / ~palaste ---------------------规则! -------- /
自行车不能自行站立,因为它是双轮胎。
- Sky Text
Correcting the obvious bug, I don''t see any problem. Arrays are
guaranteed to be contiguous, so you''ll get no alignment or offset
problems.
--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"A bicycle cannot stand up by itself because it''s two-tyred."
- Sky Text
Joona I Palaste写道:
Joona I Palaste wrote:
Thes< no*@ctually.myaddress.com>潦草地写了下面的内容:
Thes <no*@ctually.myaddress.com> scribbled the following:
大家好,
Hi all,
我有一个2d float数组,我已经向它声明了一个指针:
I have a 2d float array to which I have declared a pointer thus:
float Matrix [4] [3] = {/ * snipped initialisation * /};
float(* StoredMatrix)[4] [3];
float Matrix[4][3] = {/* snipped initialisation */ };
float (*StoredMatrix)[4][3];
以便在程序注册表中创建副本。我自己设计的。
So as to create a copy in a program "registry" of my own devising.
我按如下方式复制Marix数组:
I copy the Marix array as follows:
StoredMatrix = malloc(sizeof Matrix);
if(!StoredMatrix)
StoredMatrix = malloc(sizeof Matrix);
if(!StoredMatrix)
当然你的意思是(StoredMatrix),或者更好,
if( StoredMatrix!= NULL)在这里?你在这里写的方式是要求麻烦。
Surely you mean if (StoredMatrix), or better yet,
if (StoredMatrix != NULL) here? The way you''ve written here is asking
for trouble.
哎呀 - 是的,这正是我的意思。对此感到抱歉。
Oops - yes, that''s exactly what I mean. Sorry about that.
{
memcpy((void *)StoredMatrix,(void *)Matrix,sizeof Matrix);
}
{/ *采取行动* /}
{
memcpy((void*)StoredMatrix, (void*)Matrix, sizeof Matrix);
}
else
{ /* take action */ }
我的问题是这是否合法,如果是是否危险?
到目前为止,复制的版本使用[]运算符取消引用OK - 例如
使用* StoredMatrix [2] [1]等。我找不到K& R有用的主题。
My question is whether this is legal, and if so whether it is dangerous?
So far, the copied version dereferences OK using the [] operators - eg
using *StoredMatrix[2][1] etc. I''m not finding K&R helpful on the subject.
纠正这个明显的错误,我没有看到任何问题。数组保证是连续的,所以你不会对齐或偏移问题。
Correcting the obvious bug, I don''t see any problem. Arrays are
guaranteed to be contiguous, so you''ll get no alignment or offset
problems.
感谢您的回复。 />
Thes。
Thanks for the reply.
Thes.
2003年11月10日星期一16:50:13 +0000,Thes写道:
On Mon, 10 Nov 2003 16:50:13 +0000, Thes wrote:
我按如下方式复制Marix数组:
[snip] memcpy((void *)StoredMatrix,(void *)Matrix,sizeof Matrix);
I copy the Marix array as follows: [snip] memcpy((void*)StoredMatrix, (void*)Matrix, sizeof Matrix);
如果你有#included string.h,那么无需转换为无效。
你应该这样做。
The casts to void are unnecessary if you have #included string.h, which
you should do.
这篇关于通过指向2d数组的指针访问。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!