一个指向数组的指针? [英] a pointer to an array?

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

问题描述

大家好,全部:

最近我在comp.lang.c上找到了这条评论" Answers to FAQ" (6.13):

如果你真的需要声明一个指向整个数组的指针,请使用

之类的int(* ap)[N];其中N是数组的大小。 (参见

也问题1.21。)如果数组的大小未知,N可以在

原则中省略,但结果类型,指向数组的指针

未知大小,没用。

但是,当我尝试编写一个示例程序时:


int main()

{

int(* arrptr)[5];

int arr [5] = {1,2,3,4,5};

arrptr = arr;

printf("%d",arrptr [0]);

}


为什么不能工作?此外,有没有人知道

阵列指针的实际用法?


祝你好运,


blue

Hi, all:
Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13):
If you really need to declare a pointer to an entire array, use
something like "int (*ap)[N];" where N is the size of the array. (See
also question 1.21.) If the size of the array is unknown, N can in
principle be omitted, but the resulting type, "pointer to array of
unknown size," is useless.
However, when I try to write a sample program like:

int main()
{
int (*arrptr)[5];
int arr[5] = {1,2,3,4,5};
arrptr = arr;
printf( "%d", arrptr[0] );
}

Why could it not work? Besides, does anyone know the real usage of
array pointers?

Best regards,

blue

推荐答案



blue schreef:

blue schreef:
全部:
最近我在comp.lang.cAnswers to FAQ中找到了这条评论。 (6.13):
如果你真的需要声明一个指向整个数组的指针,请使用类似int(* ap)[N];之类的东西。其中N是数组的大小。


在C ++中我会推荐它。不幸的是,它无效C.


(另见问题1.21。)如果数组的大小未知,N可以原则上省略,但是结果类型,指向未知大小的数组的指针,没用。

但是,当我尝试编写一个示例程序时:

int main()
{
int(* arrptr)[ 5];
int arr [5] = {1,2,3,4,5};
arrptr = arr;
printf("%d",arrptr [0]) ;
}

为什么它不起作用?


因为它不是C.

int main()
{
int * arrptr;
int arr [5] = {1,2,3,4,5};
arrptr = arr;
printf("%d",arrptr [0]);
}

此外,有没有人知道数组指针的实际用法?
Hi, all:
Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13):
If you really need to declare a pointer to an entire array, use
something like "int (*ap)[N];" where N is the size of the array.
In C++ i would recommend it. Unfortunately, it''s not valid C.

(See also question 1.21.) If the size of the array is unknown, N can in
principle be omitted, but the resulting type, "pointer to array of
unknown size," is useless.
However, when I try to write a sample program like:

int main()
{
int (*arrptr)[5];
int arr[5] = {1,2,3,4,5};
arrptr = arr;
printf( "%d", arrptr[0] );
}

Why could it not work?
Coz'' it''s not C.
int main()
{
int *arrptr;
int arr[5] = {1,2,3,4,5};
arrptr = arr;
printf( "%d", arrptr[0] );
}
Besides, does anyone know the real usage of array pointers?




它们有很多用途。例如,printf通常需要一个

数组指针,因为它的第一个参数是%d。在你的情况下。你可以通过arrptr = arr + i计算指向数组中任何元素的指针。

除了它们总是指向某个有效的块

内存(只要它们在范围内),数组指针与

任何其他指针没有区别。


问候,


Kleuske



There are many uses for them. printf, for instance, usually takes an
array pointer as it''s first parameter, "%d" in your case. You can
calculate a pointer to any element in the array by arrptr=arr + i.
Except for the fact that they always point to some valid block of
memory (as long as they''re in scope), array-pointers do not differ from
any other pointer.

regards,

Kleuske


blue写道:
全部:如果你真的需要声明一个指向整个数组的指针,请使用类似int(* ap)[N];之类的东西。其中N是数组的大小。 (参见
同样问题1.21。)如果数组的大小未知,则可以省略N,但结果类型,指向未知大小的数组的指针, ;没用。

但是,当我尝试写一个示例程序时:

#include< stdio.h>

这是_not_无偿。

int main()
{
int(* arrptr)[5];
int arr [5] = {1,2,3,4 ,5};
arrptr = arr;


这是什么?如果你有

int * p,我;

然后你用

p =& i;

所以,你的编译器应该告诉你

arrptr = arr;

是错误的。使用上面的方法,

arrptr =& arr;

是对的。

printf("%d",arrptr [0]) ;


而不是p [0],你也可以写* p。所以,基本上,你是
正在尝试使用%d打印出一个数组。我希望你能够意识到这是一个愚蠢的想法。

你可能想要的是

arrptr [0] [0]

或者,在这种情况下,

(* arrptr)[0]


注意:您也可以使用arrptr访问数组N的一个

数组5的int - 也许这有助于你更好地理解

[0] [0]......

另一件事:为了使这个程序可移植,将''\ n''

附加到你的上一个输出,即%d \ n或者单独的putchar(''\ n'');

你忘了

返回0; }

为什么它不起作用?此外,有没有人知道
数组指针的实际用法?
Hi, all:
Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13):
If you really need to declare a pointer to an entire array, use
something like "int (*ap)[N];" where N is the size of the array. (See
also question 1.21.) If the size of the array is unknown, N can in
principle be omitted, but the resulting type, "pointer to array of
unknown size," is useless.
However, when I try to write a sample program like:
#include <stdio.h>
This is _not_ gratuitous.
int main()
{
int (*arrptr)[5];
int arr[5] = {1,2,3,4,5};
arrptr = arr;
What is that? If you have
int *p, i;
then you use
p = &i;
So, your compiler should tell you that
arrptr = arr;
is wrong. Using the above,
arrptr = &arr;
is right.
printf( "%d", arrptr[0] );
Instead of p[0], you can also write *p. So, essentially, you
are trying to print out an array using %d. I hope you are
aware that this is a stupid idea.
What you probably want is
arrptr[0][0]
or, in this case,
(*arrptr)[0]

Notes: You could also use arrptr to access an array N of an
array 5 of int -- maybe this helps you better to understand
the "[0][0]"...
Another thing: To make this program portable, append ''\n''
to your last output, i.e. "%d\n" or a separate putchar(''\n'');
You forgot to
return 0; }

Why could it not work? Besides, does anyone know the real usage of
array pointers?




见上文。是的。


干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de address。



See above. Yes.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


kl ***** @ xs4all.nl 写道:
blue schreef:
blue schreef:
全部:
最近我在comp.lang上发现了这条评论。 c常见问题解答 (6.13):
如果你真的需要声明一个指向整个数组的指针,请使用类似int(* ap)[N];之类的东西。其中N是数组的大小。
Hi, all:
Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13):
If you really need to declare a pointer to an entire array, use
something like "int (*ap)[N];" where N is the size of the array.



在C ++中我会推荐它。不幸的是,它不是有效的C.



In C++ i would recommend it. Unfortunately, it''s not valid C.




这是错误的。


在comp.lang.c中,我不正式了解C ++,但正确地使用了

,这个绝对是有效的C - 之所以

,整个事情都进入了_comp.lang.c_常见问题解答。


另请参阅我对该主题的其他回复。

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de address。



This is wrong.

In comp.lang.c, I do not officially know about C++ but, used
correctly, "this" is definitely valid C -- the reason why
the whole thing went into the _comp.lang.c_ FAQ.

See also my other reply to this thread.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


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

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