如何初始化char *? [英] How to initialize a char*?

查看:223
本文介绍了如何初始化char *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




很多次,我看到编译警告:你使用了char *而没有

启动它,可能在代码如下:


------------

char * ptr;


func(...,ptr);

----------


如何正确初始化char *?我曾经使用


char * ptr ="" ;;


但被告知这不好。


有谁可以向我解释为什么,以及什么是启动它的好方法?


谢谢,

彼得

解决方案

Peter< yb *** @ yahoo.com>这样说:

char * ptr;
func(...,ptr);


这很糟糕 - ptr未初始化(甚至不是NULL),因此将其传递给

函数要么无意义要么是灾难邀请,这是一个错误

无论如何。

如何正确初始化char *?我以前用过
char * ptr ="" ;;


取决于你在做什么。你可能被告知

因为它应该是


const char * ptr ="" ;;


为什么?因为是一个常量空字符串;试图修改它

邀请灾难。因此,指向这样的字符串文字的指针(因此它们被称为'b $ b')应该被声明为const;那样的话,如果你试图改变它的价值,那么编译器可以告诉你你的错误。

任何人都可以向我解释为什么,以及什么是好方法启动它?




完成。除非你有特殊的理由不这样做,否则你应该使用std :: strings来获得
所以你不必担心前面的

的东西。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。


彼得写道:




很多次,我见过编译警告:你使用了char *而没有
启动它,可能就像这样的代码:

------------
char * ptr;

func(...,ptr);
----------

如何正确初始化char * ?我曾经使用过

char * ptr ="" ;;

但被告知这不好。

任何人都可以向我解释为什么,以及什么是启动它的好方法?




我怀疑它是不是因为它不是很清楚是什么ptr是

用于:它有一个指向一个常量字符串的值,该字符串不应该被修改为
,但可以是。


当指针需要时,你可以将其初始化为:


char * ptr = NULL;


然后,更明显的是指针已经初始化并指向

什么都没有,但是当它指向某个东西时,目标将是

可修改。


"彼得" < YB *** @ yahoo.com>在留言中写道

新闻:40 ****** @ rpc1284.daytonoh.ncr.com ...



所以很多次,我看到编译警告:你使用了char *而没有
启动它,可能就像这样的代码:

---------- -
char * ptr;

func(...,ptr);
----------

如何正确初始化char *?我曾经使用过

char * ptr ="" ;;

但被告知这不好。


const char * ptr ="" ;;


但这会初始化''ptr''的地址为

字符串文字的第一个字符"。

无法修改此指针的目标。


如果要创建初始化一个指针,你想要稍后改变它的价值,只需将它初始化为某个字符的地址,或者为0(NULL)。


char * ptr = 0;

任何人都可以向我解释为什么,以及什么是启动它的好方法?



这真的取决于你在做什么。作为一般的

规则,我尝试不创建一个对象,直到我有一个有意义的

值可用于初始化它。如果没有这样的

值,我会''默认''初始化它,即对于一个带有NULL的

指针。

你确定使用std :: string

对象而不是char指针会更好吗? std :: string对象是

在创建时自动初始化。


std :: string s; / *用空字符串初始化* /


-Mike


Hi,

So many times, I have seen compile warning: "you used a char* without
initilize it", probably on the code like this:

------------
char* ptr;

func(..., ptr);
----------

How to properly initialize a char*? I used to use

char* ptr = "";

but was told this is not good.

Can anybody explain to me why, and what''s a good way to initilize it?

Thanks,
Peter

解决方案

Peter <yb***@yahoo.com> spoke thus:

char* ptr;
func(..., ptr);
This is bad - ptr is uninitialized (not even NULL), so passing it to a
function is either pointless or an invitation to disaster, a mistake
in any case.
How to properly initialize a char*? I used to use char* ptr = "";
Depends on what you''re doing with it. You probably were told that
because it should be

const char *ptr="";

Why? Because "" is a constant empty string; trying to modify it
invites disaster. Therefore, pointers to such string literals (so
they''re called) should be declared const; that way if you try to
modify what it points to the compiler can inform you of your mistake.
Can anybody explain to me why, and what''s a good way to initilize it?



Done. Unless you have a specific reason not to, though, you should be
using std::strings so you won''t have to worry about the preceding
stuff.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


Peter wrote:


Hi,

So many times, I have seen compile warning: "you used a char* without
initilize it", probably on the code like this:

------------
char* ptr;

func(..., ptr);
----------

How to properly initialize a char*? I used to use

char* ptr = "";

but was told this is not good.

Can anybody explain to me why, and what''s a good way to initilize it?



I''d suspect that it isn''t recommended because it isn''t really clear what ptr is
used for: it has a value pointing to a constant string that shouldn''t be
modified, but can be.

When and where a pointer is necessary, you can initialize it as:

char* ptr = NULL;

Then, it is more obvious that the pointer has been initialized and points to
nothing, but that when it does point to something, the destination will be
modifiable.


"Peter" <yb***@yahoo.com> wrote in message
news:40******@rpc1284.daytonoh.ncr.com...

Hi,

So many times, I have seen compile warning: "you used a char* without
initilize it", probably on the code like this:

------------
char* ptr;

func(..., ptr);
----------

How to properly initialize a char*? I used to use

char* ptr = "";

but was told this is not good.
const char *ptr = "";

But this initializes ''ptr'' with the address of
the first character of the string literal "".
The target of this pointer cannot be modified.

If you want to create an initialize a pointer whose
value you want to change later, just initialize it
to the address of some character, or to 0 (NULL).

char *ptr = 0;

Can anybody explain to me why, and what''s a good way to initilize it?



It really depends upon what you''re doing. As a general
rule, I try not to create an object until I have a meaningful
value available with which to initialize it. If no such
value is available, I''ll ''default'' initialize it, i.e. for a
pointer, with NULL.

Are you sure you wouldn''t be better off using a std::string
object instead of a char pointer? std::string objects are
automatically initialized when created.

std::string s; /* initialized with empty string */

-Mike


这篇关于如何初始化char *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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