char * p =" longenough&quot ;; [英] char *p = "longenough";

查看:63
本文介绍了char * p =" longenough&quot ;;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是获取点指针的健康方式吗?

char * p =" longenough" ;;

问候,

jasper

Is this a healthy way to get a pointer to point ?
char *p = "longenough";
regards,
jasper

推荐答案

Jasper Dozer< ja ********* @ hotmail.com>潦草地写道:
Jasper Dozer <ja*********@hotmail.com> scribbled the following:
这是一个获得点指针的健康方式吗?
char * p =" longenough" ;;
问候,
jasper
Is this a healthy way to get a pointer to point ?
char *p = "longenough";
regards,
jasper




取决于你所说的健康。如果你这样做,你可以访问

字符串longenough每次p都在范围内阅读,但你不能用b
来便于写作。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)--------------------------- \

| 飞翔的柠檬树中的金鸡王G ++ FR FW + M-#108 D + ADA N +++ |

| http://www.helsinki.fi/~palaste W ++ B OP + |

\ -----------------------------------------芬兰的规则! ------------ /

当一个男人对一个女人说脏话时,那就是性骚扰。当一个女人对男人说脏话时,那是'每分钟14.99美元+本地电话费!'

- Ruben Stiller



Depends what you mean by "healthy". If you do this, you can access the
string "longenough" for reading every time p is in scope, but you can''t
portably access it for writing.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"When a man talks dirty to a woman, that''s sexual harassment. When a woman talks
dirty to a man, that''s 14.99 per minute + local telephone charges!"
- Ruben Stiller


Jasper Dozer写道:
Jasper Dozer wrote:
这是一个获得点指针的健康方法吗?
char * p =" longenough";
问候,
jasper
Is this a healthy way to get a pointer to point ?
char *p = "longenough";
regards,
jasper




编号使用''const'':

const char * p =" longenough" ;;

文字应视为常量数据。

指针的类型应为指向const数据的指针。一个

可能会争辩说指针

的值也应该是常数;但我会把它留作一种风格

问题。


如果你想修改字符串文字,那么试试:

char p [] =" Modify Me .;;

数组表示法将告诉编译器创建一个数组,长度由数组大小决定字面值

常数,并将数据复制到数组中。


-

Thomas Matthews

C ++新闻组欢迎辞:
http:/ /www.slack.net/~shiva/welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍



No. Use ''const'':
const char * p = "longenough";
The literal should be treated as constant data. The
pointer should be of type "pointer to const data". One
could argue that fact that the value of the pointer
should be constant too; but I''ll leave that as a style
issue.

If you want to modify the string literal, then try:
char p[] = "Modify Me.";
The array notation will tell the compiler to create
an array, length determined by the size of the literal
constant, and copy the data into the array.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


Jasper Dozer写道:
Jasper Dozer wrote:

这是一种获取点指针的健康方法吗?
char * p =" longenough";

Is this a healthy way to get a pointer to point ?
char *p = "longenough";



取决于你想做什么。你知道什么是字符串文字,

以及如何使用它们并且可能不会被使用?您选择的文字

的名称让我感到困扰,看起来您认为这是一种快速的方式来动态分配空间用于其他用途。请不要这样做!


Brian Rodenborn


Depends on what you want to do. Do you know what string literals are,
and how they may be used and may not be used? The name of the literal
you''ve chosen bothers me, it seems like you think this is a quick way to
dynamically allocate space for some other use. DO NOT DO THAT!


Brian Rodenborn


这篇关于char * p =&quot; longenough&quot ;;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆