char str[] = "hello world";和char *str = "hello world";区别
2012-11-15 15:56
1726 查看
char *strA() { char str[] = "hello world"; return str; }str[] = "hello world"//"hello world"常量字符串在内存中有两份拷贝,一份在动态分配的栈中,一份在静态存储区,str[]数组为函数内部局部变量,存储在栈上,在strA()函数退出时,栈要清空,局部变量的内存也被清空
char *strA() { char *str = "hello world"; return str; }
char *str = "hello world"//一份拷贝,"hello world"是常量字符串存在静态数据区,把该字符串常量存在的静态数据区的首地址赋给指针str,所以strA()函数退出时,该字符串常量所在内存不会被回收,故能通过指针访问;
char *strA() { static char str[] = "hello world";//static变量被保存在静态存储区而不是堆栈, return str; }
此题参考《程序员面试宝典》
及/article/6913678.html
http://hi.baidu.com/pasan/item/735c5d0bbee290e0fe240da7
相关文章推荐
- char str[] = "hello world";和char *str = "hello world";区别
- char p[] = "hello world" 与 char *p = "Hello world" 的区别
- 关于char * c="hello"与char c[]="hello"的区别
- char str[] = {"abcd"}和 char* str = {"abcd"}的区别
- [C++ VC MFC] char* str="heelo"与char str[]="heelo"区别
- char c[5]="hello"和char *p="hello"区别
- String str = "hello"; 与 String str = new String("hello"); 有什么区别?
- 关于char * c="hello"与char c[]="hello"的区别(面试宝典)
- char *Str;Str="abcdef";和char Str[]="abcdef";有什么区别??
- 关于笔试题 "在函数里char p[] = "hello world" 与 char *p = "Hello world""
- “Hello, world" 粗略比较 C语言和Objective-C的区别
- char p[] = "hello world" 与 char *p = "Hello world" 的差异
- char *myString = "Hello-World!";和 char myString[] = "Hello-World!";
- 关于 char p[]="hello world";与char *p="hello world";
- 关于char * c="hello"与char c[]="hello"的区别
- 【转载】关于char * c="hello"与char c[]="hello"的区别
- C中char str[] = "hello word";与char *str = "hello word";
- 面试题--字符串指针与字符串数组的区别char* st="abc;与char* str[]="abc";的异同
- char *s1 = "hello"和char s2[]="world"的区别
- c++真狗屎系列:char *str = "abcde"; 与 char str[] ="abcde"; 有何区别?