本文介绍了指针数组和堆栈数组之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 区别在于: char str1 [] =" wxyz" ;; char * str2 =" abcd" ;; 我可以这样做: str2 = str1 但我不能这样做: str1 = str2 (技术上是不是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; 或 str2 =& str [0]; 但是那是'不是C的发明者决定如何做到这一点而且 - 代替了引入了一些自动转换。 但是 str1 = str2; 仍然是语法错误,因为''str1''不是指针而且不能 被视为一个指针,因为它有一个完全不同的 类型。 问候,Jens - \ Jens Thoms Toerring ___ [email protected] \ __________________________ http://toerring.de 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,是部长 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 = str1but I can''t do this:str1 = str2(isn''t str1 technically a pointer?)Thanks 解决方案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 placewhere a value is required (e.g. if ''str1'' is an argument of afunction call) it gets replaced automatically by a pointer tothe first element of that array. But that doesn''t change any-thing about the "nonpointerness" of ''str1'', it''s an array andremains to be an array until it goes out of scope.In contrast, ''str2'' is a pointer, initialized to point to thestring literal "abcd" (that could very well be in read-onlymemory). Since ''str2'' is a pointer you can assign it a differentvalue, e.g. by usingstr2 = str1;This works because in this case on the right hand side of theasignment a value is required and now "the rule" applies, i.e.that if an array is used in a context where a value is neededit is replaced by a pointer to its first element.This automatic conversion is actually not much different fromwhat happens when you writeint a = 1.2;Since on the right hand side an int value is required the doublevalue you have there is automatically converted to an int value.C could in principle refrain from doing such automatic conversionsand require that you explicitely state your intent likeint a = ( int ) 1.2;orstr2 = &str[ 0 ];but that''s not how the inventors of C decided to do it and in-stead introduced some automatic conversions.Butstr1 = str2;is still a syntax error since ''str1'' is not a pointer and can''tbe treated like a pointer because it has a completely differenttype.Regards, Jens--\ Jens Thoms Toerring ___ [email protected]\__________________________ http://toerring.destr1 is an array; str2 is a pointer.Right. str1, an array expression, is implicitly converted to apointer in most contexts, including this one.Right, you can''t assign to an array.No, str1 is an array. Arrays are not pointers; pointers are notarrays. This is probably the most common misconception about C.Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>. Feelfree to post again with more specific questions if you''re stillconfused 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"str1 is an array; str2 is a pointer.Right. *str1, an array expression, is implicitly converted to apointer in most contexts, including this one.Right, you can''t assign to an array.No, str1 is an array. *Arrays are not pointers; pointers are notarrays. *This is probably the most common misconception about C.Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>. *Feelfree to post again with more specific questions if you''re stillconfused after that.--Keith Thompson (The_Other_Keith) [email protected] *<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 :) 这篇关于指针数组和堆栈数组之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-03 18:13