** p vs. p [] [] [英] **p vs. p[][]

查看:76
本文介绍了** p vs. p [] []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

p被声明为2D数组,p [m] [n],与** p基本相同吗?如果

我尝试将main()中的p声明为p [m] [n],然后将其作为

参数传递给具有相应参数的例程(函数)* * p,我

得到不兼容的指针类型。如果我将相应的

参数声明为(* p)[m],它就可以工作。


如果我将main()中的p声明为** p并做双嵌套for循环

malloc()一次一行,然后使用双索引,[] [],

访问和分配元素malloc {}''ed ealier,它工作正常。


总而言之,p声明为p [m] [n]与** p不同br />
有时但是其他时间?


--- John

Is p declared as a 2D array, p[m][n], essentially the same as **p? If
I try declararing p in main() as p[m][n] and then pass it as an
argument to a routine (function) with corresponding parameter **p, I
get "incompatible pointer type". If I declare the corresponding
parameter as (*p)[m], it works.

If I declare p in main() as **p and do the double nested for-loops
malloc()''ing one row at a time and then use double indexing, [][], to
access and assign elements malloc{}''ed ealier, it works fine.

So to sum it up, p declared as p[m][n] is not the same as **p
sometimes but is other times?

---John

推荐答案

jski写道:
jski wrote:

p被声明为2D数组,p [m] [n],与** p基本相同?
Is p declared as a 2D array, p[m][n], essentially the same as **p?



号码为什么?

No. Why?


如果

我尝试声明p in main()为p [m] [n],然后将其作为

参数传递给具有相应参数** p的例程(函数),I

get" ;不兼容的指针类型。
If
I try declararing p in main() as p[m][n] and then pass it as an
argument to a routine (function) with corresponding parameter **p, I
get "incompatible pointer type".



确切地说。

Precisely.


如果我将相应的

参数声明为( * p)[m],它有效。
If I declare the corresponding
parameter as (*p)[m], it works.



是。

Yes.


如果我将main()中的p声明为** p并执行双精度操作嵌套for循环

malloc()一次一行,然后使用双索引,[] [],

访问并分配元素malloc {} 'ed ealier,它运作正常。
If I declare p in main() as **p and do the double nested for-loops
malloc()''ing one row at a time and then use double indexing, [][], to
access and assign elements malloc{}''ed ealier, it works fine.



是。

Yes.


总而言之,p声明为p [m] [n]有时是** p

不一样,但有时候呢?
So to sum it up, p declared as p[m][n] is not the same as **p
sometimes but is other times?



它是_never_相同。您正确地指出,如果您声明''** p''

并正确分配内存,您可以使用''p访问您手动制作的

数组[i] [j]''语法。同时,如果你声明

''q [10] [20]'',你也可以使用''q [i] [j]''来访问这个C数组。 。

基于此,你似乎得出的结论是''p''和''q''

必须是相同的。在这种情况下。你的结论是不正确的。尽管

事实上你可以在两个数据结构上使用''[i] [j]'',但

结构仍然不同,并且内部语义含义为

''[i] [j]''仍然不同。


作为一个松散相关的例子,考虑这些声明


int a = 0;

double * b = 0;


请注意,在这些声明之后,您可以通过
做''++ a''和''++ b''。这会让你得出结论''int''必须是'/ b $ b'与''double *''相同吗?我希望不是。仅仅因为你可以将

相同的运算符应用于两个不同的对象并不意味着这些

对象必须相同。


同样的逻辑适用于上面我的例子中应用于''p''和

''q''的''[]''运算符对。仅仅因为你可以将''[] []''应用于''p''

和''q''并不意味着''p''是相同的作为''q''。你有什么?
这个案例是一个2D数组'''',通过使用内置的

数组阵列方法和一个2D数组实现'p''使用

手册实现数组指针技术。实现

2D阵列的这两种方法在内部几乎没有共同之处,这就是为什么你不能将
转换为另一种。

有关详细信息,您可能还想查看

中的一些帖子这些搜索结果

http://www.google.com/search?hl= en& r ... +数组+指针


-

祝你好运,

Andrey Tarasevich

It is _never_ the same. You correctly noted that if you declare ''**p''
and allocate memory properly, you are able to access your manually-made
array using the ''p[i][j]'' syntax. At the same time, if you declare
''q[10][20]'', you are also able to access this C-array using ''q[i][j]''.
Based on this you seem to be jumping to conclusion that the ''p'' and ''q''
must be "the same" in this case. Your conclusion is incorrect. Despite
the fact that you can use the ''[i][j]'' on both data structures, the
structures remain different, and the internal semantic meaning of that
''[i][j]'' remains different.

Consider, as a loosely related example, these declarations

int a = 0;
double* b = 0;

Note that after these declarations you can increment both objects by
doing ''++a'' and ''++b''. Does that make you conclude that ''int'' must be
the same thing as ''double*''? I hope not. Just because you can apply the
same operator(s) to two different objects does not mean that these
objects must be "the same".

The same logic applies to the pair of ''[]'' operators applied to ''p'' and
''q'' in my example above. Just because you can apply ''[][]'' to both ''p''
and ''q'' does not mean that ''p'' is "the same" as ''q''. What you have in
this case is a 2D array ''q'' implemented by using the built-in
array-of-arrays approach, and a 2D array ''p'' implemented by using a
"manual" array-of-pointers technique. These two methods of implementing
2D arrays have very little in common internally, which is why you can''t
convert one to another.

For more details, you might also want to take a look at some posts in
these search results

http://www.google.com/search?hl=en&r...+array+pointer

--
Best regards,
Andrey Tarasevich


> p是否被声明为2D数组,p [m] [n],与** p基本相同?如果


No.


试图进入数组元素 ** p需要

存在指向p类型的指针,你没有,所以尝试取消引用它们的b $ b很可能是引起麻烦(例如,段错误或

从内存中的某处返回随机垃圾)。
>Is p declared as a 2D array, p[m][n], essentially the same as **p? If

No.

Attempting to get to the "array elements" of **p requires the
existence of pointers-to-type-of-p, which you don''t have, so trying
to dereference them is likely to cause trouble (e.g. segfault or
return random crap from somewhere in memory).

>我尝试在main()中声明p为p [m] [n]然后将其作为
参数传递给具有相应参数** p的例程(函数),I
得到不兼容的指针类型。如果我将相应的
参数声明为(* p)[m],它就可以了。
>I try declararing p in main() as p[m][n] and then pass it as an
argument to a routine (function) with corresponding parameter **p, I
get "incompatible pointer type". If I declare the corresponding
parameter as (*p)[m], it works.



使用声明为x []而不是* x的函数参数将

起作用,但仅限于一个级别,因为数组作为函数参数

默默地变成指向数组第一个元素的指针。

The use of function parameters declared as x[] instead of *x will
work, but only to one level, because an array as a function argument
is silently turned into a pointer to the first element of the array.


>如果我在main中声明p ()作为** p并执行双嵌套for循环
malloc()一次一行,然后使用双索引,[] [],来访问和分配元素malloc {}''ed ealier,它工作正常。
>If I declare p in main() as **p and do the double nested for-loops
malloc()''ing one row at a time and then use double indexing, [][], to
access and assign elements malloc{}''ed ealier, it works fine.



是的,但你所拥有的是* NOT * 2-D数组(类型p [m] [n];),

它不像二维阵列那样布局,如果你试图假装它

是一个通过将它传递到一个预期的功能,预计会发生灾难。

这可能是N + 1个不连续的部分。它需要比真正的2-D数组(指针的内存)更多的内存
。一个真正的2-D

数组不能像那样工作。 (但仅仅因为它不是一个真正的b $ b b-2阵列,使用它没有任何问题 - 甚至还有一些优点。不要撒谎并尝试将它作为一个使用。)


对于真正的二维数组,

函数的函数和调用者需要完全同意数组的第一个维度,

(对于C90,这是一个编译的常量值:你可能不会在b / d中传递
运行时并期望像真正的

2-D数组一样使用它或者p [j] [k]将为j!= 0选择错误的值。对于

指向一维数组指针的向量,这不是问题,尽管你还是要担心超出范围。在这里,你可以在运行时传递维度。

Yes, but what you have there is *NOT* a 2-D array (type p[m][n];),
it''s not laid out like a 2-D array, and if you try to pretend it
is one by passing it to a function expecting one, expect disaster.
It''s likely to be in N+1 discontiguous pieces. It takes more memory
than a real 2-D array (the memory for the pointers). A real 2-D
array does not work like that. (But just because it''s not a real
2-D array, there''s nothing wrong with using it - there are even
some advantages. Just don''t lie and try to use it as one.)

For a real 2-D array, both the function and the caller of the
function need to agree on the first dimension of the array exactly,
(and for C90, this is a compiled-in constant value: you may NOT
pass in the dimensions at runtime and expect to use it like a real
2-D array) or p[j][k] will pick up the wrong value for j != 0. For
the vector of pointers to 1-D arrays, that isn''t an issue, although
you still have to worry about going out of bounds. Here, you can
pass in the dimensions at runtime.


>所以总结一下,p声明为p [m] [n]有时和**有时不一样,但有时候呢?
>So to sum it up, p declared as p[m][n] is not the same as **p
sometimes but is other times?



不,它不一样。期间。

No, it''s not the same. Period.


首先,感谢您的回复。那也是安德烈。


然后使用(* p)[m]作为参数(在某些例程中)并传递p

声明因为main()中的p [m] [n]在技术上是不正确的,即使它是
有效吗?


--- John

11月17日,1:56 * am,gordonb.16 ... @ burditt.org(Gordon Burditt)写道:
First, thanks for replying. That goes to Andrey as well.

Then the use of (*p)[m] as a parameter (in some routine) and passing p
declared as p[m][n] in main() is technically incorrect, even though it
works?

---John
On Nov 17, 1:56*am, gordonb.16...@burditt.org (Gordon Burditt) wrote:

p被声明为2D数组,p [m] [n],与** p基本相同? *如果
Is p declared as a 2D array, p[m][n], essentially the same as **p? *If



No. *


试图进入数组元素 ** p需要

存在指向p类型的指针,你没有,所以尝试取消引用它们的b $ b很可能是导致麻烦(例如,段错误或

从内存中的某处返回随机垃圾)。


No. *

Attempting to get to the "array elements" of **p requires the
existence of pointers-to-type-of-p, which you don''t have, so trying
to dereference them is likely to cause trouble (e.g. segfault or
return random crap from somewhere in memory).


我尝试将main()中的p声明为p [ m] [n]然后将它作为

参数传递给具有相应参数** p的例程(函数),I

get不兼容的指针类型。如果我将相应的

参数声明为(* p)[m],它就可以了。
I try declararing p in main() as p[m][n] and then pass it as an
argument to a routine (function) with corresponding parameter **p, I
get "incompatible pointer type". If I declare the corresponding
parameter as (*p)[m], it works.



使用声明为x []而不是* x的函数参数将

起作用,但仅限于一个级别,因为数组作为函数参数

默默地变成指向数组第一个元素的指针。


The use of function parameters declared as x[] instead of *x will
work, but only to one level, because an array as a function argument
is silently turned into a pointer to the first element of the array.


如果我在main()中声明p作为** p并执行双嵌套for循环

malloc()一次一行,然后使用双索引[] [],来

访问并分配元素malloc {}''ed ealier,它工作正常。
If I declare p in main() as **p and do the double nested for-loops
malloc()''ing one row at a time and then use double indexing, [][], to
access and assign elements malloc{}''ed ealier, it works fine.



是的,但你所拥有的是* NOT *一个二维数组(类型p [m] [n];),

它不像二维阵列那样布局,如果你试图假装它

是一个通过将它传递到一个预期的功能,预计会发生灾难。

这可能是N + 1个不连续的部分。 *它需要比真正的2-D数组(指针的内存)更多的内存。 *真正的2-D

数组不能像这样工作。 *(但仅仅因为它不是一个真正的
2-D阵列,使用它没有任何问题 - 甚至还有一些优点。* b $ b一些优点。*只是不要撒谎并尝试将其作为一个使用。)


对于一个真正的二维数组,功能和来电者

函数需要完全同意数组的第一维,

(对于C90,这是一个编译的常量值:*你可能不会

传入运行时的维度和期望使用它就像一个真实的

2-D数组)或p [j] [k]将为j!= 0选取错误的值。*对于

指向1-D数组的指针向量,这不是问题,尽管

你还是要担心超出界限。 *在这里,你可以在运行时传递维度。


Yes, but what you have there is *NOT* a 2-D array (type p[m][n];),
it''s not laid out like a 2-D array, and if you try to pretend it
is one by passing it to a function expecting one, expect disaster.
It''s likely to be in N+1 discontiguous pieces. *It takes more memory
than a real 2-D array (the memory for the pointers). *A real 2-D
array does not work like that. *(But just because it''s not a real
2-D array, there''s nothing wrong with using it - there are even
some advantages. *Just don''t lie and try to use it as one.)

For a real 2-D array, both the function and the caller of the
function need to agree on the first dimension of the array exactly,
(and for C90, this is a compiled-in constant value: *you may NOT
pass in the dimensions at runtime and expect to use it like a real
2-D array) or p[j][k] will pick up the wrong value for j != 0. *For
the vector of pointers to 1-D arrays, that isn''t an issue, although
you still have to worry about going out of bounds. *Here, you can
pass in the dimensions at runtime.


总而言之,p声明为p [m] [ n]有时候和** p

不一样,但有时候呢?
So to sum it up, p declared as p[m][n] is not the same as **p
sometimes but is other times?



不,它不一样。 *期。


No, it''s not the same. *Period.


这篇关于** p vs. p [] []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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