为什么这么少人都知道数组和指针之间的区别。 [英] Why do so few people know the difference between arrays and pointers.

查看:72
本文介绍了为什么这么少人都知道数组和指针之间的区别。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



只是一个沮丧的问题/观察。


我仔细阅读了Peter Van Der Linden的书,名为

专家C编程 (深C秘密)。特别是

章节题为:

4:令人震惊的真相:C阵列和指针不一样!

9:关于数组的更多信息

10:关于指针的更多信息


让我感到震惊的是每个程序员

我已经采访过的大学毕业生认为指针

和数组也是一样的。


他们甚至告诉我,在下面的代码中,''arr''是指向整数数组的
a指针。或者他们告诉我''arr''是一个指向''int''的
指针。如果不是这样的话。


int arr [100];


变量''arr''IS和数组100个整数...这就是它的类型人。

就像下面的''我'的类型是''int''。


int i;


和以下''ch''的类型是''char *''。


char * ch =" string";


上面''arr''的TYPE是''100个整数数组''。那就是为什么你要
必须按如下方式声明指向它的指针:


int(* p)[100];


p =& arr; //有效

p = arr; //无效(编译器会警告你)但是因为

地址碰巧是相同的。


注意:在表达式中使用数组时或者作为R值,

操作的结果

产生一个指向数组中第一个元素的指针。因此:


int * ip = arr; //有效:arr变成一个int指针(指向数组中第一个元素的指针)

//不是因为ip是''int *''因为数组''arr''在表达式中使用

作为R值。
//作为R值。


大学里的C老师都没有做好自己的工作!


-

使用Opera革命性的电子邮件客户端: http://www.opera.com/m2/

解决方案

" Me" <博*** @ bogus.com>在消息中写道


"令人震惊的真相:C阵列和指针不是相同的!



数组标签衰减到指针,所以


void foo (int * ptr);


无效栏(无效)

{

int arr [100];


foo(arr);

}


用指向整数数组的指针调用foo。


这就是你需要知道的关于C数组指针等价的所有内容。


如果你开始使用多维数组和花哨的声明你

应该得到所有你想要的东西。





2004年6月8日星期二22:30:49 +0100 ,Malcolm

< ma ***** @ 55bank.freeserve.co.uk>写道:

" Me" <博*** @ bogus.com>在消息中写道


"令人震惊的真相:C阵列和指针不是相同的!
数组标签衰减到指针,所以

void foo(int * ptr);

void bar(void)
{arr [100];

foo(arr);
}

调用foo用指向整数数组的指针。




实际上你错了......可能你只是误输了。


它使用指向int的指针调用foo ....而不是指向

int数组的指针。

指针指向的是这样的是
中第一个用100个整数分配的数组的int,但''foo''不知道。 foo()只是

认为

它是一个''int''指针。

这就是你需要知道的关于C数组指针的所有内容等价。

如果你开始使用多维数组和花哨的声明,你应该得到所有的东西。




-

使用Opera的革命性电子邮件客户端: http://www.opera.com/m2/


自称自称的东西我写道:

只是一个沮丧的问题/观察。

我深入阅读了Peter Van Der Linden所着的题为专家C编程的书。 (深C秘密)。
特别是章节标题:
4:令人震惊的真相:C阵列和指针不一样!
9:关于阵列的更多信息
10:更多关于指针的信息


显而易见的夸张。

让我感到震惊的是,每个''程序员'走出水面的事实我接受过采访的大学
认为指针和数组是一回事。

他们甚至告诉我,在下面的代码中,
'' arr''是一个指向整数数组的指针。
或者他们告诉我''arr''指向''int''。
如果不是这样的话。

int arr [100];

变量''arr''IS和100个整数的数组...这就是它的类型人。
就像以下我的类型是''int''。

int i;

以下是''ch'的类型' '字符*'。
char * ch =" string";

上面''arr''的TYPE是''100个整数数组''。
那个'为什么你必须按如下方式声明指向它的指针:

int(* p)[100];

p =& arr; //有效
p = arr; //无效(编译器会警告你)
//但是有效,因为地址恰好相同。

注意:在表达式中使用数组或作为R值,
操作的结果
产生一个指向数组中第一个元素的指针。因此:

int * ip = arr; //有效:arr变成int指针
//(指向数组中第一个元素的指针)
//不是因为ip是''int *''
//但是因为数组''arr''在表达式中被用作一个R值。

大学里的C老师都没有做好自己的工作! / blockquote>


你现在感觉好多了吗?



Just a question/observation out of frustration.

I read in depth the book by Peter Van Der Linden entitled
"Expert C Programming" (Deep C Secrets). In particular the
chapters entitled:
4: The Shocking Truth: C Arrays and Pointers Are NOT the Same!
9: More about Arrays
10: More about Pointers

What blows me out of the water is the fact that ''every'' programmer
comming out of college that I''ve interviewed thinks that pointers
and arrays are the same thing.

They go so far as to tell me that, in the following code, ''arr'' is
a pointer to an array of integers. Or they tell me that ''arr'' is a
pointer to an ''int''. When this is not the case at all.

int arr[100];

The variable ''arr'' IS and array of 100 integers...THATS ITS TYPE MAN.
Just like the TYPE of ''i'' in the following is ''int''.

int i;

and the type of ''ch'' in the following is ''char *''.

char *ch = "string";

The TYPE of ''arr'' above is ''an array of 100 integers''. That''s WHY you
have to declare a pointer to it as follows:

int (*p)[100];

p = &arr; // Valid
p = arr; // Invalid (compiler will warn you) but works because the
address happens to be the same.

Note: When you use an array in an expression or as an R-Value, the result
of the
operation yields a pointer to the first element in the array. Thus:

int *ip = arr; // Valid: arr turns into an int pointer (a pointer to the
first element in the array)
// Not because ip is an ''int *'' be because the array ''arr'' is being
used in an expression
// as an R-Value.

Man the C teachers in college aren''t doing their job!

--
Using Opera''s revolutionary e-mail client: http://www.opera.com/m2/

解决方案

"Me" <bo***@bogus.com> wrote in message


" The Shocking Truth: C Arrays and Pointers Are NOT the
Same!


Array labels decay to pointers, so

void foo(int *ptr);

void bar(void)
{
int arr[100];

foo(arr);
}

calls foo with a pointer to an array of integers.

This ia all you need to know about C array pointer equivalence.

If you start using multi-dimensional arrays and fancy declarations you
deserve all that is coming to you.




On Tue, 8 Jun 2004 22:30:49 +0100, Malcolm
<ma*****@55bank.freeserve.co.uk> wrote:

"Me" <bo***@bogus.com> wrote in message


" The Shocking Truth: C Arrays and Pointers Are NOT the
Same!
Array labels decay to pointers, so

void foo(int *ptr);

void bar(void)
{
int arr[100];

foo(arr);
}

calls foo with a pointer to an array of integers.



Actually you are mistaken...probably you just mis-typed.

It calls foo with a pointer to an int.... not a pointer to an array of
ints.
It just so happens that what the pointer is pointing to is the first int in
the allocated array of 100 ints, but ''foo'' doesn''t know that. foo() just
thinks
it is an ''int'' pointer.

This ia all you need to know about C array pointer equivalence.

If you start using multi-dimensional arrays and fancy declarations you
deserve all that is coming to you.



--
Using Opera''s revolutionary e-mail client: http://www.opera.com/m2/


Something that calls itself Me wrote:

Just a question/observation out of frustration.

I read in depth the book by Peter Van Der Linden entitled
"Expert C Programming" (Deep C Secrets).
In particular the chapters entitled:
4: The Shocking Truth: C Arrays and Pointers Are NOT the Same!
9: More about Arrays
10: More about Pointers
Obvious hyperbole.
What blows me out of the water is the fact that
''every'' programmer comming out of college that I''ve interviewed
thinks that pointers and arrays are the same thing.

They go so far as to tell me that, in the following code,
''arr'' is a pointer to an array of integers.
Or they tell me that ''arr'' is a pointer to an ''int''.
When this is not the case at all.

int arr[100];

The variable ''arr'' IS and array of 100 integers...THATS ITS TYPE MAN.
Just like the TYPE of ''i'' in the following is ''int''.

int i;

and the type of ''ch'' in the following is ''char*''.

char* ch = "string";

The TYPE of ''arr'' above is ''an array of 100 integers''.
That''s WHY you have to declare a pointer to it as follows:

int (*p)[100];

p = &arr; // Valid
p = arr; // Invalid (compiler will warn you)
// but works because the address happens to be the same.

Note: When you use an array in an expression or as an R-Value,
the result of the operation
yields a pointer to the first element in the array. Thus:

int* ip = arr; // Valid: arr turns into an int pointer
// (a pointer to the first element in the array)
// Not because ip is an ''int *''
// but because the array ''arr'' is being used
// in an expression as an R-Value.

Man the C teachers in college aren''t doing their job!



Do you feel better now that you got that off your chest?


这篇关于为什么这么少人都知道数组和指针之间的区别。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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