指针数组和堆栈数组之间的区别? [英] difference between a pointer array and a stack array?

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

问题描述

区别在于:


char str1 [] =" wxyz" ;;

char * str2 =" abcd" ;;


我可以这样做:

str2 = str1


但我不能这样做:

str1 = str2


(技术上是不是str1指针?)

谢谢

解决方案

Ahmad Humayun<啊********* @ gmail.comwrote:


区别如下:


char str1 [] =" wxyz" ;;

char * str2 =" abcd" ;;


我可以这样做:

str2 = str1


但我不能这样做:

str1 = str2


(isn'' t str1技术上是指针?)



编号''str1''是一个包含5个字符的数组,初始化为字符

' 'w'',''x'',''y'',''z''和''\'''。只有在一个地方使用''str1''

,其中需要一个值(例如,如果''str1''是

函数调用的参数)它由指向

的指针自动替换该数组的第一个元素。但这并没有改变任何关于非指针性的事情。 ''str1'',它是一个数组而且

仍然是一个阵列,直到它超出范围。


相比之下,' 'str2''是一个指针,初始化为指向

字符串文字abcd (很可能是只读的内存)。由于''str2''是一个指针,你可以为它指定一个不同的

值,例如:通过使用


str2 = str1;


这是有效的,因为在这种情况下在
$ b $的右侧b,需要一个值,现在是规则。适用,即
如果在需要值的上下文中使用数组

它将被指向其第一个元素的指针替换。


这种自动转换与

差别不大。当你写的时候会发生什么


int a = 1.2;


因为在右侧需要一个int值,你所拥有的双值

值会自动转换为int值。


C原则上可以避免进行这样的自动转换

并且要求你明确说出你的意图


int a =(int)1.2; < br $>




str2 =& str [0];


但是那是'不是C的发明者决定如何做到这一点而且 -
代替了
引入了一些自动转换。


但是


str1 = str2;


仍然是语法错误,因为''str1''不是指针而且不能

被视为一个指针,因为它有一个完全不同的

类型。

问候,Jens

-

\ Jens Thoms Toerring ___ jt@toerring.de

\ __________________________ http://toerring.de


Ahmad Humayun < ah ********* @ gmail.comwrites:


区别的是:


char str1 [] =" wxyz";

char * str2 =" abcd" ;;



str1是一个数组; str2是一个指针。


我可以这样做:

str2 = str1



对。 str1,一个数组表达式,在大多数情况下隐式转换为

指针,包括这个指针。


但我不能这样做:

str1 = str2



对,你不能分配给一个数组。


(技术上是不是str1指针?)



不,str1是一个数组。数组不是指针;指针不是
数组。这可能是关于C的最常见的误解。


阅读comp.lang.c FAQ的第6部分,< http://www.c-faq.com/> 。感觉

可以免费发布更多具体问题,如果你仍然感到困惑,那么
混淆了。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti .net / ~kst>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长


5月26日,1 :04 * am,Keith Thompson< ks ... @ mib.orgwrote:


Ahmad Humayun< ahmad.hu ... @ gmail.comwrites:


区别为:


char str1 [] =" wxyz" ;;

char * str2 =" abcd" ;;



str1是一个数组; str2是一个指针。


我可以这样做:

str2 = str1



对。 * str1,一个数组表达式,在大多数情况下隐式转换为

指针,包括这个指针。


但我可以''做到这一点:

str1 = str2



对,你不能分配给一个数组。


(技术上是不是str1指针?)



不,str1是一个数组。 *数组不是指针;指针不是
数组。 *这可能是关于C的最常见的误解。


阅读comp.lang.c FAQ的第6部分,< http://www.c-faq.com/> ;。 *感觉

可以免费再次发布更具体的问题,如果你仍然在那之后混淆



-

Keith Thompson(The_Other_Keith)ks ... @ mib.org *< http://www.ghoti.net/~kst>

诺基亚

我们必须做点什么。 *这是事情。 *因此,我们必须这样做。

* * - Antony Jay和Jonathan Lynn,是部长



谢谢Jens和Antony ....这个信息真的很有帮助:)


快乐编码:)


Whats the difference between:

char str1[] = "wxyz";
char* str2 = "abcd";

I can do this:
str2 = str1

but I can''t do this:
str1 = str2

(isn''t str1 technically a pointer?)
Thanks

解决方案

Ahmad Humayun <ah*********@gmail.comwrote:

Whats the difference between:

char str1[] = "wxyz";
char* str2 = "abcd";

I can do this:
str2 = str1

but I can''t do this:
str1 = str2

(isn''t str1 technically a pointer?)

No. ''str1'' is an array of 5 chars, initialized to the characters
''w'', ''x'', ''y'', ''z'' and ''\0''. Only if ''str1'' is used in a place
where a value is required (e.g. if ''str1'' is an argument of a
function call) it gets replaced automatically by a pointer to
the first element of that array. But that doesn''t change any-
thing about the "nonpointerness" of ''str1'', it''s an array and
remains to be an array until it goes out of scope.

In contrast, ''str2'' is a pointer, initialized to point to the
string literal "abcd" (that could very well be in read-only
memory). Since ''str2'' is a pointer you can assign it a different
value, e.g. by using

str2 = str1;

This works because in this case on the right hand side of the
asignment a value is required and now "the rule" applies, i.e.
that if an array is used in a context where a value is needed
it is replaced by a pointer to its first element.

This automatic conversion is actually not much different from
what happens when you write

int a = 1.2;

Since on the right hand side an int value is required the double
value you have there is automatically converted to an int value.

C could in principle refrain from doing such automatic conversions
and require that you explicitely state your intent like

int a = ( int ) 1.2;

or

str2 = &str[ 0 ];

but that''s not how the inventors of C decided to do it and in-
stead introduced some automatic conversions.

But

str1 = str2;

is still a syntax error since ''str1'' is not a pointer and can''t
be treated like a pointer because it has a completely different
type.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de


Ahmad Humayun <ah*********@gmail.comwrites:

Whats the difference between:

char str1[] = "wxyz";
char* str2 = "abcd";

str1 is an array; str2 is a pointer.

I can do this:
str2 = str1

Right. str1, an array expression, is implicitly converted to a
pointer in most contexts, including this one.

but I can''t do this:
str1 = str2

Right, you can''t assign to an array.

(isn''t str1 technically a pointer?)

No, str1 is an array. Arrays are not pointers; pointers are not
arrays. This is probably the most common misconception about C.

Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>. Feel
free to post again with more specific questions if you''re still
confused after that.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


On May 26, 1:04*am, Keith Thompson <ks...@mib.orgwrote:

Ahmad Humayun <ahmad.hu...@gmail.comwrites:

Whats the difference between:

char str1[] = "wxyz";
char* str2 = "abcd";


str1 is an array; str2 is a pointer.

I can do this:
str2 = str1


Right. *str1, an array expression, is implicitly converted to a
pointer in most contexts, including this one.

but I can''t do this:
str1 = str2


Right, you can''t assign to an array.

(isn''t str1 technically a pointer?)


No, str1 is an array. *Arrays are not pointers; pointers are not
arrays. *This is probably the most common misconception about C.

Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>. *Feel
free to post again with more specific questions if you''re still
confused after that.

--
Keith Thompson (The_Other_Keith) ks...@mib.org *<http://www.ghoti.net/~kst>
Nokia
"We must do something. *This is something. *Therefore, we must do this.."
* * -- Antony Jay and Jonathan Lynn, "Yes Minister"

Thanks Jens and Antony....this info was really really helpful :)

Happy coding :)


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

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