const char * = new char [6] [英] const char* = new char[6]
问题描述
大家好
我有
const char * p =" Hello";
所以,这里的内存不是由C ++编译器为p分配的,因此我不能访问p [0]将内容修改为Kello
p [ 0] ='''K''; //运行时错误
所以我做了
const char * p = new char [6];
但是我如何将其初始化为Hello?我的要求是 - 我想要
a const char *初始化后来想要修改内容。
我知道如下所示的方式
< br $>
const char p [] =" hello";
const_cast< char&>(p [0])='''K''; //好吧
但是如何用指针实现这个?
我所知道的是 - 当我做的时候C编译器分配内存>
const char * =" hello";
但C ++编译器不这样做。欢迎任何帮助。
谢谢
SS
< blockquote> SS写道:
我有
const char * p =" Hello" ;;
所以,这里的内存不是由C ++编译器为p分配的,因此我不能访问p [0]将内容修改为Kello
p [0] =''K''; //运行时错误
所以我做了
const char * p = new char [6];
但是我如何将其初始化为Hello?我的要求是 - 我想要
一个const char *初始化,后来想要修改内容。
无论你尝试什么,这都将是未定义的行为。
修改一个声明为const的对象只是一个禁忌。
我知道如下所示的方式
const char p [] =" hello" ;
const_cast< char&>(p [0])='''K''; // OK
不,不行。这是未定义的行为。它可以在你的平台上运行 - 现在为
。
但如何用指针实现这一点?
我所知道的是 - C编译器在我执行时分配内存
const char * =" hello";
但C ++编译器不这样做。欢迎任何帮助。
Best
Kai-Uwe Bux
< blockquote> SS写道:
大家好
我有
const char * p =" Hello";
所以,这里的内存不是由C ++编译器为p
分配的
p是只有一个指针。编译器确实为指针分配存储器
本身。
因此我无法访问p [0]将内容修改为 Kello
p [0] =''K''; //运行时错误
编译器为字符串文字分配存储空间,但作为常量。所以
你可以访问它,但你不能写它。
所以我做了
const char * p = new char [6];
但是如何将其初始化为Hello?
你不能直接初始化它。您可以使用strcpy将文字
复制到您的数组中,例如:
strcpy(p," Hello");
但是,除非你删除''const'',否则没有演员就不会工作。
顺便说一句:别忘了删除你的一旦你不再需要它就行了。
我的要求是 - 我想要一个const char *初始化然后想要
修改内容。
如果你想修改它,不要把它变成常量。
我知道一个方式如下所示
const char p [] =" hello";
const_cast< char&>(p [0])='' K ''; // OK
如果要写入数组,为什么要使数组为const?只做:
char p [] =" hello";
p [0] =''K'';
但如何用指针实现这一点?
char p [] =" hello";
char * ptr = p;
ptr [ 0] ='''K'';
但为什么你真的想在这里有指针?
什么我知道是 - C编译器在我这样做时分配内存
const char * =" hello" ;;
但是C ++编译器不这样做。
C处理上述代码行的方式与C ++的处理方式没有区别。
另一个问题:为什么你使用原始指针来代替
std :: string?
SS写道:
>
const char * p =" Hello";
所以,这里内存不是由C ++编译器为p分配的,因此我不能访问p [0]将内容修改为Kello
p [0] ='' K ''; //运行时错误
不,这不正确。首先,你需要了解两件事。
P是一个指针。它是一个保存char的地址的变量。
p本身的内存是在声明的上下文中分配的。
(很可能是函数的本地函数)包含它。
你好在这种情况下,实现在一些
未指定的位置分配为(const)字符数组,这个值是
转换为指针并存储在p中。
首先,您的错误应该发生在运行时。编译器
应该在编译时拒绝存储到
a const char的访问冲突。
即使你不是在这里使用const:
char * p =" Hello";
p [0] =''K'';
是未定义的行为。不允许使用字符串文字内存
要更改(编译器允许此单据是历史记录)。
>
所以我做了
const char * p = new char [6 ];
但是我如何将其初始化为Hello?我的要求是 - 我想要
一个const char *初始化,后来想要修改内容。
Yoj不能将它初始化,但你可以复制到它。
strcpy(p,hello);
>
我知道如下所示的方式
const char p [] = " hello";
const_cast< char&>(p [0])='''K''; //好吧
但是如何用指针实现这个?
我所知道的是 - 当我做的时候C编译器分配内存>
const char * =" hello";
但C ++编译器不这样做。欢迎任何帮助。
Nope,C和C ++在这方面实际上表现相同。
Hi Everyone
I have
const char *p = "Hello";
So, here memory is not allocated by C++ compiler for p and hence I
cannot access p[0] to modify the contents to "Kello"
p[0] = ''K''; // error at runtime
So I did
const char *p = new char[6];
But then how do I initialize it to "Hello"? My requirement is - I want
a const char* initialised and later want to modify the contents.
I know a way as written below
const char p[] = "hello";
const_cast<char&>(p[0]) = ''K''; //OK
But how to acheive this with pointers?
What I know is - C compiler allocates memory when I do
const char* = "hello";
But C++ compiler does not do that. Any help is welcome.
Thanks
SS
S S wrote:
I have
const char *p = "Hello";
So, here memory is not allocated by C++ compiler for p and hence I
cannot access p[0] to modify the contents to "Kello"
p[0] = ''K''; // error at runtime
So I did
const char *p = new char[6];
But then how do I initialize it to "Hello"? My requirement is - I want
a const char* initialised and later want to modify the contents.That will always be undefined behavior, regardless of what you try.
Modifying an object that was declared const is simply a no-no.
I know a way as written below
const char p[] = "hello";
const_cast<char&>(p[0]) = ''K''; //OKNope, not OK. It''s undefined behavior. It may work on your platform -- for
now.
But how to acheive this with pointers?
What I know is - C compiler allocates memory when I do
const char* = "hello";
But C++ compiler does not do that. Any help is welcome.
Best
Kai-Uwe Bux
S S wrote:
Hi Everyone
I have
const char *p = "Hello";
So, here memory is not allocated by C++ compiler for pp is only a pointer. The compiler does allocate storage for the pointer
itself.
and hence I cannot access p[0] to modify the contents to "Kello"
p[0] = ''K''; // error at runtimeThe compiler allocates storage for the string literal, but as a constant. So
you can access it, but you''re not allowed to write to it.
So I did
const char *p = new char[6];
But then how do I initialize it to "Hello"?You can''t directly initialize it. You can use strcpy to copy the literal
over to your array, like:
strcpy(p, "Hello");
However, that won''t work without a cast unless you remove the ''const''.
Btw: Don''t forget to delete your array as soon as you don''t need it anymore.
My requirement is - I want a const char* initialised and later want to
modify the contents.If you want to modify it, don''t make it const.
I know a way as written below
const char p[] = "hello";
const_cast<char&>(p[0]) = ''K''; //OKWhy do you make the array const if you want to write to it? Just do:
char p[] = "hello";
p[0] = ''K'';
But how to acheive this with pointers?char p[] = "hello";
char* ptr = p;
ptr[0] = ''K'';
But why do you actually want to have a pointer here?
What I know is - C compiler allocates memory when I do
const char* = "hello";
But C++ compiler does not do that.There is no difference between the way C treats the above line of code and
the way C++ does.
Another question: Why do you use raw pointers to char instead of
std::string?
S S wrote:
>
const char *p = "Hello";
So, here memory is not allocated by C++ compiler for p and hence I
cannot access p[0] to modify the contents to "Kello"
p[0] = ''K''; // error at runtimeNo, that is not correct. First, you need to understand two things.
P is a pointer. It is a variable that holds the address of a char.
The memory for p itself is allocated in the context it was declared
(most likely local to a the function that contains it).
"Hello" in this context is allocated by the implementation at some
unspecified location as a array of (const) chars, this value is
converted to a pointer and stored in p.
First, your error should have happened NOT at runtime. The compiler
should reject at compile time the access violation of storing into
a const char.
Even if you were not to use const here:
char* p = "Hello";
p[0] = ''K'';
is undefined behavior. The string literal memory is not allowed
to be changed (the fact the compiler lets this slip is historical).>
So I did
const char *p = new char[6];
But then how do I initialize it to "Hello"? My requirement is - I want
a const char* initialised and later want to modify the contents.Yoj can''t initalialize it, but you can copy into it.
strcpy(p, "hello");
>
I know a way as written below
const char p[] = "hello";
const_cast<char&>(p[0]) = ''K''; //OK
But how to acheive this with pointers?
What I know is - C compiler allocates memory when I do
const char* = "hello";
But C++ compiler does not do that. Any help is welcome.
Nope, C and C++ actually behave identically in this aspect.
这篇关于const char * = new char [6]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!