b*********n 发帖数: 1258 | 1 int main(int argc, char* argv[])
{
char *a="abcde";
a[0] = 'f';
cout<
return 0;
}
compile没问题(Visual Studio 6)
如果定义char a[]="abcde";
就没问题
C++里面char *a和char a[]不都是一样的吗?
谢谢 |
z******i 发帖数: 59 | 2 Could it be:
char *a ---> pointer to read only section
char a[]---> array in stack, initialized with "abcd", which is in read only
section.
【在 b*********n 的大作中提到】 : int main(int argc, char* argv[]) : { : char *a="abcde"; : a[0] = 'f'; : cout<: return 0; : } : compile没问题(Visual Studio 6) : 如果定义char a[]="abcde"; : 就没问题
|
m**s 发帖数: 221 | 3
是不是abcde被看成只读常量?
【在 b*********n 的大作中提到】 : int main(int argc, char* argv[]) : { : char *a="abcde"; : a[0] = 'f'; : cout<: return 0; : } : compile没问题(Visual Studio 6) : 如果定义char a[]="abcde"; : 就没问题
|
j*****k 发帖数: 1198 | 4 allocate memory for a first before you assign it with some value
【在 b*********n 的大作中提到】 : int main(int argc, char* argv[]) : { : char *a="abcde"; : a[0] = 'f'; : cout<: return 0; : } : compile没问题(Visual Studio 6) : 如果定义char a[]="abcde"; : 就没问题
|
d*****a 发帖数: 110 | 5 c++? if you really want c++, use string instead of char * and you'll get rid
of most of this kind trouble.
first basic things to use c++ instead of c:
alloc, malloc/free -> new/delete
char [], char* -> string
array -> vector |
s9 发帖数: 1192 | 6 a[0] = 'f'出错是因为它试图去修改一个不允许修改的字符串常量。
char *a="abcde"; a是一个处于堆栈的指针,其值是"abcde"这个字符串常量的地址。
长度是4(32 bit PC)。
char a[]="abcde" a是一个处于堆栈的数组,其长度取决于其所包含的字符串的长度。
【在 b*********n 的大作中提到】 : int main(int argc, char* argv[]) : { : char *a="abcde"; : a[0] = 'f'; : cout<: return 0; : } : compile没问题(Visual Studio 6) : 如果定义char a[]="abcde"; : 就没问题
|
c*********t 发帖数: 2921 | 7 check this link
http://c-faq.com/decl/strlitinit.html
【在 b*********n 的大作中提到】 : int main(int argc, char* argv[]) : { : char *a="abcde"; : a[0] = 'f'; : cout<: return 0; : } : compile没问题(Visual Studio 6) : 如果定义char a[]="abcde"; : 就没问题
|
s******e 发帖数: 431 | 8 应该有Warning吧?应该这么写
const char* str="abcd"; |
d****y 发帖数: 701 | 9 char *a;是一个指针,没有ASSIGN存储空间.
char *a="abcde"; 是给这个指针一个值"abcde","abcde"可能会被TRUNCTED,depending
on your system.
指针和数组是有本质区别的. |
c****n 发帖数: 105 | 10 I just did some test. See the program below:
===============================
#include
#include
#include
int main(int argc, char* argv[])
{
char *a="abcde";
char b[]="abcde";
char* c=(char*)malloc(6);
strcpy(c, a);
printf("%lx, %lx, %lx\n", a, b, c);
printf("%c %c %c\n", a[0], b[0], c[0]);
a[0] = 'f';
printf("%s\n", a);
return 0;
}
==============================
output:
80486d0, bfc74b96, 804b008
a a a
Segmentation fault
char *a="abcde", the compiler generate a str |
|
|
k****f 发帖数: 3794 | 11 C老问题拉,去精华区看看吧
sizeof(a)和sizeof(b)是不一样的
【在 c****n 的大作中提到】 : I just did some test. See the program below: : =============================== : #include : #include : #include : int main(int argc, char* argv[]) : { : char *a="abcde"; : char b[]="abcde"; : char* c=(char*)malloc(6);
|
b**y 发帖数: 74 | 12 It is a pure C question rather than C++.
char *a = "abcde";
a is not in stack or heap. It is in the memory which is read only. Any
attempt to change the content of a is wrong. |
b******n 发帖数: 592 | 13 a is just a pointer. It is initialised to point to read-only memory. Changin
g a is fine, changing the content of memory pointed by a is not okay
【在 b**y 的大作中提到】 : It is a pure C question rather than C++. : char *a = "abcde"; : a is not in stack or heap. It is in the memory which is read only. Any : attempt to change the content of a is wrong.
|
y*****a 发帖数: 171 | 14 下面是汇编代码,string "abcde" is in readonly section (.rodata). 除非你修改
gcc default linker script, you cannot write to this address
.file "l.c"
.section .rodata
.LC0:
.string "abcde"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
addl $15, %eax
addl $15, %eax
shrl $4, %eax
sall $4, %eax
|
d*****a 发帖数: 110 | 15 niu!
【在 y*****a 的大作中提到】 : 下面是汇编代码,string "abcde" is in readonly section (.rodata). 除非你修改 : gcc default linker script, you cannot write to this address : .file "l.c" : .section .rodata : .LC0: : .string "abcde" : .text : .globl main : .type main, @function : main:
|