char *按值传递 [英] char * pass-by-value

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

问题描述




抱歉新手问题;阅读常见问题后,我仍然离开

想知道这是否合法:


void strip(char * in)

{

in [strlen(in)-1] =''\ 0'';

返回;

}


我认为是这样,因为变量''in''的内存位置是正确传递的,所以strlen()函数寻找''\\ \\ 0''

获得其值的字符。这是真的吗?


谢谢,


Andrej

解决方案

< blockquote> Andrej Prsa写道:



抱歉新手问题;在阅读常见问题解答后,我仍然离开
想知道这是否合法:

void strip(char * in)
{
in [strlen(in) - 1] =''\ 0'';
返回;
}
我认为是,因为变量''in''的内存位置是字符来获取它的值。这是真的吗?




是的,这是完全合法的,是的,这就是strlen的工作方式。


-

====================================== ============ =============

试图减少信号上的不必要的噪音 '...


免责声明:


我提供的任何评论/代码可能= = = 100%便携,也不是

语义正确[读 - ''不是100%迂腐正确'']。

我不太关心这个,我认为它是

与大多数访客相同。但是,请放心,任何

''必要''(?)更正几乎肯定会出现v.soon

[读 - ''加上他们认为合适的噪音,*一个学究*将会很快就会在

''。


警告:请务必阅读标签。没有旁边的细节

过滤器提供。远离儿童。不要点燃。

====================================== ============ =============


Andrej Prsa写道:


抱歉新手问题;在阅读常见问题解答后,我仍然离开
想知道这是否合法:

void strip(char * in)
{
in [strlen(in) - 1] =''\ 0'';
返回;
}
我认为是,因为变量''in''的内存位置是字符来获取它的值。这是真的吗?

谢谢,

Andrej



void strip2(char in [])

{

in [strlen(in)-1] =''\ 0'';

return;

}




相同
void strip1(char * in)

{

*(in + strlen(in)-1)=''\''';

返回;

}

NB

char * s与char s []不等同,如下所示:


char string [32] =" litte string" ;;

char * ptr = string;

sizeof(string)==> 32

sizeof(ptr)==> 4 / *(在一个4字节指针的机器上)* /

NB
$ strite(in)-1中的
因为减法而不是很好...

用指针减去是不是很有意义......

在这种情况下它就像(在+(strlen(in)-1))


xavier


Andrej Prsa写道:



抱歉新手问题;在阅读常见问题解答后,我仍然离开
想知道这是否合法:

void strip(char * in)
{
in [strlen(in) - 1] =''\ 0'';
返回;
}
我认为是,因为变量''in''的内存位置是字符来获取它的值。这是真的吗?




字符串`in`最好至少包含一个字符,或者`in`

必须指向一个字符(或更多)字符存入合法存储。因为

strlen(in)== 0意味着你有`in [-1] = 0`。


BOOM今天 - 如果你是幸运。否则,BOOM明天。


-

Chris理解是一把三刃剑 Dollin


Hi,

sorry for the newbie question; after reading the FAQ, I am still left
wondering if this is legal:

void strip (char *in)
{
in[strlen(in)-1] = ''\0'';
return;
}

I figure it is, because the memory location of the variable ''in'' is
passed properly and I guess that the strlen() function seeks the ''\0''
character to obtain its value. Is this true?

Thanks,

Andrej

解决方案

Andrej Prsa wrote:

Hi,

sorry for the newbie question; after reading the FAQ, I am still left
wondering if this is legal:

void strip (char *in)
{
in[strlen(in)-1] = ''\0'';
return;
}

I figure it is, because the memory location of the variable ''in'' is
passed properly and I guess that the strlen() function seeks the ''\0''
character to obtain its value. Is this true?



Yes, it''s perfectly legal, and yes, that''s how strlen works.

--
================================================== =============
In an attempt to reduce ''unwanted noise'' on the ''signal'' ...

Disclaimer:

Any comment/code I contribute might =NOT= be 100% portable, nor
semantically correct [read - ''not 100% pedantically correct''].
I don''t care too much about that though, and I reckon it''s the
same with most ''visitors'' here. However, rest assured that any
''essential'' (?) corrections WILL almost certainly appear v.soon
[read - ''to add noise as they see fit, *a pedant* will be along
shortly''].

WARNINGS: Always read the label. No beside-the-point minutiae
filter supplied. Keep away from children. Do not ignite.
================================================== =============


Andrej Prsa wrote:

Hi,

sorry for the newbie question; after reading the FAQ, I am still left
wondering if this is legal:

void strip (char *in)
{
in[strlen(in)-1] = ''\0'';
return;
}

I figure it is, because the memory location of the variable ''in'' is
passed properly and I guess that the strlen() function seeks the ''\0''
character to obtain its value. Is this true?

Thanks,

Andrej


void strip2 (char in[])
{
in[strlen(in)-1] = ''\0'';
return;
}

same as

void strip1 (char *in)
{
*(in+strlen(in)-1) = ''\0'';
return;
}
N.B.
char * s is not equivalent to char s[] as you can see with the following :

char string[32] = "litte string";
char *ptr = string;
sizeof (string) ==> 32
sizeof (ptr) ==> 4 /* (on a machine with 4 byte pointers) */
N.B.
in+strlen(in)-1 is not very nice because of the substract ...
it''s not very meaningfull to substract with pointers ...
in this case it''s like (in + (strlen(in)-1))

xavier


Andrej Prsa wrote:

Hi,

sorry for the newbie question; after reading the FAQ, I am still left
wondering if this is legal:

void strip (char *in)
{
in[strlen(in)-1] = ''\0'';
return;
}

I figure it is, because the memory location of the variable ''in'' is
passed properly and I guess that the strlen() function seeks the ''\0''
character to obtain its value. Is this true?



The string `in` had better have at least one character in it, or `in`
must point one (or more) character(s) into legal storage. Because
strlen(in) == 0 would mean you had `in[-1]=0`.

BOOM today - if you''re lucky. Otherwise, BOOM tomorrow.

--
Chris "understanding is a three-edged sword" Dollin


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

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