char *p="abcdef"和char s[]="abcdef"存储在哪里的问题!
2011-06-09 23:44
225 查看
char *p="abcdef"; //"abcdef" 静态存储区,是常量区,不可改, p[2]企图通过p这个指针改变常量区的内容,所以你这个程序编译通过,运行会出错
char s[]="abcdef";//"abcdef" 静态存储区, 你赋值给s[]这个变量后,改变s[2],所以不出错。
p, s 在同一个区,如果是局部变量,就是在栈里面。
"abcdef"; //静态存储区,是常量区,不可改
char *p="abcdef"; 只是将p指向了该字符串常量而已
char s[]="abcdef"; 在栈上分配了字符串数组,并且将字符串常("abcdef")量的值拷贝到该数组中
,该数组的值是可以修改的,但其被没有修改原来的字符串常量
1.32: What is the difference between these initializations?
char a[] = "string literal";
char *p = "string literal";
My program crashes if I try to assign a new value to p[i].
A:A string literal can be used in two slightly different ways. As
an array initializer (as in the declaration of char a[] in the
question), it specifies the initial values of the characters in
that array. Anywhere else, it turns into an unnamed, static
array of characters, which may be stored in read-only memory,
and which therefore cannot necessarily be modified. In an
expression context, the array is converted at once to a pointer,
as usual (see section 6), so the second declaration initializes
p to point to the unnamed array's first element.
(For compiling old code, some compilers have a switch
controlling whether string literals are writable or not.)
See also questions 1.31, 6.1, 6.2, 6.8, and 11.8b.
References: K&R2 Sec. 5.5 p. 104; ISO Sec. 6.1.4, Sec. 6.5.7;
Rationale Sec. 3.1.4; H&S Sec. 2.7.4 pp. 31-2.
char s[]="abcdef";//"abcdef" 静态存储区, 你赋值给s[]这个变量后,改变s[2],所以不出错。
p, s 在同一个区,如果是局部变量,就是在栈里面。
"abcdef"; //静态存储区,是常量区,不可改
char *p="abcdef"; 只是将p指向了该字符串常量而已
char s[]="abcdef"; 在栈上分配了字符串数组,并且将字符串常("abcdef")量的值拷贝到该数组中
,该数组的值是可以修改的,但其被没有修改原来的字符串常量
1.32: What is the difference between these initializations?
char a[] = "string literal";
char *p = "string literal";
My program crashes if I try to assign a new value to p[i].
A:A string literal can be used in two slightly different ways. As
an array initializer (as in the declaration of char a[] in the
question), it specifies the initial values of the characters in
that array. Anywhere else, it turns into an unnamed, static
array of characters, which may be stored in read-only memory,
and which therefore cannot necessarily be modified. In an
expression context, the array is converted at once to a pointer,
as usual (see section 6), so the second declaration initializes
p to point to the unnamed array's first element.
(For compiling old code, some compilers have a switch
controlling whether string literals are writable or not.)
See also questions 1.31, 6.1, 6.2, 6.8, and 11.8b.
References: K&R2 Sec. 5.5 p. 104; ISO Sec. 6.1.4, Sec. 6.5.7;
Rationale Sec. 3.1.4; H&S Sec. 2.7.4 pp. 31-2.
相关文章推荐
- char *p="abcdef"和char s[]="abcdef"存储在哪里?
- 关于 "java 如何调用Oracle存储过程中的动态refcursor结果集" 问题
- "std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::str() const"问题的解决办法
- vs 错误1------"无法启动程序'...debug/abc.exe',系统找不到指定文件"的问题!
- 面试题--字符串指针与字符串数组的区别char* st="abc;与char* str[]="abc";的异同
- 字符串赋值问题 char *str="abc4Efg";
- 共享一文件夹提示"服务器存储空间不足,无法处理此命令"的问题解决方法
- Win7,Win8中"XXX is not accessible"的问题(应用程序默认配置文件存储位置)
- char *p="%s"; printf(p);的问题
- “搞笑”的C数组5["abcdef"]表示方法
- char *p = new char; 和 char *p2 = "abcdef"的区别
- 编码 -- char 1= '中' 2 byte, 中文string a="中" 3字节问题
- error C2664: “int CWnd::GetWindowTextW(LPTSTR,int) const”: 不能将参数 1 从“char [10]”转"相关的问题
- char *s="string"和char s[]="string"的区别
- 对两个字符a和b进行初始化:char a[]="ABCDEF";char b[]={'A','B','C','D','E','F'};则以下叙述正确的是:
- 无法启动程序'...debug/abc.exe',系统找不到指定文件"的问题
- 字符串str1、str2,测试str2中字符是否都包含在str1中。例如:str1="abcdef",str2="aaaaccc",则返回true
- char *s="string"和char s[]="string"的区别
- char *Str;Str="abcdef";和char Str[]="abcdef";有什么区别??
- 编码 -- char 1= '中' 2 byte, 中文string a="中" 3字节问题