const char * [英] const char*

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

问题描述



在一些函数原型中为什么他们使用const char *代替

const char这里需要const是什么?

如果我们必须使用const

以及为什么他们不使用char * const而是因为它是正确的方式

来指定一个const字符串(我认为)?


in some function prototypes why they use const char* instead of
const char what''s the need for const here is?
if we must use const
and why they don''t use char *const instead because it''s the right way
to designate a const string (i think)?

推荐答案

Jrdman< ahmed.bo ... @ gmail.comwrote:
Jrdman <ahmed.bo...@gmail.comwrote:

hi,

in some function * prototypes为什么他们使用const * char *

而不是const char

in some function *prototypes why they use const *char*
instead of const char



因为通过值传递的单个字符与指向字符数组的指针不一样。

Because a single character passed by value is not the same
thing as a pointer to an array of characters.


这里对const的需求是什么?
what''s the need for const here is?



在const char *中,它表示

指向的字符是const。

In const char * it signifies that the characters being
pointed to are const.


如果我们必须使用const以及为什么他们不使用char * const

而是因为它是正确的方式

来指定一个const字符串(我认为)?
if we must use const and why they don''t use char *const
instead because it''s the right way
to designate a const string (i think)?



当你说''常量字符串'时你怎么看?

它是一个永不移动的字符串,还是一个字符串那个

不应该改变吗?


严格来说,const意味着''不能写''。它没有b $ b意味着''不能改变''。 const的目的是防止

反对尝试更改你没有的数据(或者b
给自己)更改的权限。


-

Peter

What do you think of when you say ''constant string''?
Is it a string that never moves, or a string that
shouldn''t change?

Strictly speaking, const means ''can''t write''. It doesn''t
mean ''can''t change''. The purpose of const is to guard
against attempting to change data you do not have (or
give yourself) permission to change.

--
Peter


9月23日凌晨3点06分,Jrdman< ahmed.bo ... @ gmail .comwrote:
On Sep 23, 3:06*am, Jrdman <ahmed.bo...@gmail.comwrote:

hi,

in some function * prototypes为什么他们使用const * char *而不是

const char这里对const的需求是什么?

in some function *prototypes why they use const *char* instead of
const char what''s the need for const here is?



1.

void display(const char *);

int main(void)

{

display(" hello"); //在这种情况下,你不能使用const char。


返回0;

}

2.

const char * tString =" world" ;; //这意味着由tString指向

的字符串是常量

且无法修改。

1.
void display(const char*);
int main(void)
{
display("hello"); // In this case, you cannot use "const char".

return 0;
}
2.
const char* tString = "world"; //This means the string pointed to
by tString is constant
and cannot be modified.


On Mon ,2008年9月22日21:25:01 -0700(PDT),santoshsy

< sa ******* @ gmail.comwrote:
On Mon, 22 Sep 2008 21:25:01 -0700 (PDT), santoshsy
<sa*******@gmail.comwrote:

> 9月23日凌晨3点06分,Jrdman< ahmed.bo ... @ gmail.comwrote:
>On Sep 23, 3:06*am, Jrdman <ahmed.bo...@gmail.comwrote:

> hi ,在某些函数*原型中为什么他们使用const * char *代替
const char这里对const的需求是什么?
>
in some function *prototypes why they use const *char* instead of
const char what''s the need for const here is?


1.
void display(const char *);
int main(void)
{

display( "你好"); //在这种情况下,您不能使用const char。


1.
void display(const char*);
int main(void)
{
display("hello"); // In this case, you cannot use "const char".



当然可以将非常量字符串传递给

函数,期望指向const char的指针(例如strcmp)。

你真的想说什么?

It is certainly permissible to pass a non-constant string to a
function expecting a pointer to const char (e.g., strcmp). What did
you really mean to say?


>

返回0;
}

2.

const char * tString =" world" ;; //这意味着由tString指向
的字符串是常量的

并且无法修改。
>
return 0;
}
2.
const char* tString = "world"; //This means the string pointed to
by tString is constant
and cannot be modified.



唯一的限制是指向的对象无法通过取消引用此指针来修改




char arr [] =" world" ;;

const char * p1 = arr;

char * p2 = arr;

p1 [2] =''你'; / *无效* /

p2 [2] =''你'; / *应该没问题* /


-

删除del电子邮件

The only restriction is that the object pointed to cannot be modified
by dereferencing this pointer.

char arr[] = "world";
const char *p1 = arr;
char *p2 = arr;
p1[2] = ''u''; /* invalid */
p2[2] = ''u''; /* should be fine */

--
Remove del for email


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

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