减去字符串 [英] Subtracting strings

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

问题描述

大家好,


我有一个基本的问题,我从来没有把握过,而且我想要最终让它休息一下。 。


我有以下代码,虽然它使用字符串

库,因此可以标记为OT,实际问题我

与它周围的代码有关。这是高度删节的

版本:


const char * fname;

const char * execname =" / usr / bin / foo" ;;

char * pathname;

char * str;


....

strcpy(pathname,execname); < - 在我的平台上

我打算使用strlcpy():-)

if((str = strrchr(pathname,''/''))!= NULL)

{

* ++ str =''\''';

fname = execname +(str - pathname); < - 某种程度上,fname

最终成为foo按预期

}否则{

fname = execname;

* pathname =''\ 0'';

}


我已经检查了ascii值,对于上面的内容,然后转动了

得到的ascii代码(这似乎是连在一起的),我没有

生产foo。


感谢任何帮助,因为它正在煎炸我的大脑!
< br $> b $ b谢谢


KB。

解决方案

Krumble Bunk写道:


我有一个基本的问题,我从来没有把握过,而且我想要最终让它停下来。



(你实际上从未提出过任何问题,但我认为它是

为什么这样做?) />


我有以下代码,虽然它使用了字符串

库,因此可以标记为OT,实际问题我

与它周围的代码有关。这是高度删节的

版本:


const char * fname;

const char * execname =" / usr / bin / foo" ;;

char * pathname;

char * str;


...

strcpy(pathname,execname); < - 在我的平台上

我打算使用strlcpy():-)

if((str = strrchr(pathname,''/''))!= NULL)

{

* ++ str =''\''';

fname = execname +(str - pathname); < - 某种程度上,fname

最终成为foo按预期



`str`和`pathname`是指针。减去指针

(指向同一个对象)会产生它们之间的差异

- 添加多少到第二个以获得第一个。

它不是字符串减法。


所以在测试之后,如果成功,`str`指向最后一个`/`
`pathname`中的
。然后它会增加(和/或
写入`/`,以便`pathname`指向字符串up-to-but-not

包括`/`,即" / usr / bin")。现在,

`st`和`pathname`之间的区别 - 注意`str`指向与`pathname`相同的

对象 - 是数字

之前的字符foo在字符串中。将它添加到`execname`会得到一个指针

到foo在execname''s" / usr / bin / foo" ;.


} else {

fname = execname;

* pathname =''\''';

}


我检查了ascii值,对于上面的



为什么?无论字符集是什么,代码都可以工作。 (对于具有不同字符集的机器,它可能不是/ b $ b /有用/但是那是另外一件事。


并转动

产生的ascii代码(似乎是连在一起),我没有

产生foo。 />

感谢任何帮助,因为它正在煎炸我的大脑!



啊,好吧,这就是问题 - 你应该要求一个

/ positronic / brain,而不是/ peonic / one。


-

此后,事件可能会被忽视。 / Foundation /


Hewlett-Packard Limited注册办事处:Cain Road,Bracknell,

注册号:690597 England Berks RG12 1HN


4月17日下午3:41,Chris Dollin< chris.dol ... @ hp.comwrote:


Krumble Bunk写道:


我有一个基本的问题,我从来没有把握过,而且我想要最后的

把它放好。



(你实际上从未提出过任何问题,但我认为它是

为什么这样做?) />


我有以下代码,虽然它使用了字符串

库,因此可以标记为OT,实际问题我

与它周围的代码有关。这是高度删节的

版本:


const char * fname;

const char * execname =" / usr / bin / foo" ;;

char * pathname;

char * str;


...

strcpy(pathname,execname); < - 在我的平台上

我打算使用strlcpy():-)

if((str = strrchr(pathname,''/''))!= NULL)

{

* ++ str =''\''';

fname = execname +(str - pathname); < - 某种程度上,fname

最终成为foo按预期



`str`和`pathname`是指针。减去指针

(指向同一个对象)会产生它们之间的差异

- 添加多少到第二个以获得第一个。

它不是字符串减法。


所以在测试之后,如果成功,`str`指向最后一个`/`
`pathname`中的
。然后它会增加(和/或
写入`/`,以便`pathname`指向字符串up-to-but-not

包括`/`,即" / usr / bin")。现在,

`st`和`pathname`之间的区别 - 注意`str`指向与`pathname`相同的

对象 - 是数字

之前的字符foo在字符串中。将它添加到`execname`会得到一个指针

到foo在execname''s" / usr / bin / foo" ;.


} else {

fname = execname;

* pathname =''\''';

}


我检查了ascii值,对于上述,



为什么?无论字符集是什么,代码都可以工作。 (对于具有不同字符集的机器,它可能不是/ b $ b /有用/但是那是另外一件事。


并转动

产生的ascii代码(这似乎是连在一起的),我没有

产生foo。


感谢任何帮助,因为它正在煎炸我的大脑!



啊,好吧,这就是问题 - 你应该要求一个

/ positronic / brain,而不是/ peonic / one。


-

此后,事件可能会被忽视。 / Foundation /


Hewlett-Packard Limited注册办事处:Cain Road,Bracknell,

注册号:690597 England Berks RG12 1HN



克里斯 - 非常感谢你的帮助!很好地回答我的问题。


欢呼


KB。


Krumble Bunk< ; kr ********* @ gmail.comwrote:


我有以下代码,虽然使用了字符串

库,因此可以标记为OT,实际问题我和b $ b有关于它周围的代码。



<重新排序帖子的段落>


我检查了ascii值,对于上面的,并转动

产生的ascii代码(似乎是连在一起),我不会产生foo。



看看ASCII值不能解释这个谜团;-)你不是

减去字符串 (无论这可能意味着什么),你减去了

指针。这是允许的,只要涉及的指针指向同一个对象(就像他们在这里所做的那样,因为他们都指向某个地方

到''pathname'指向的字符串中')然后在两个指针之间返回

元素。


这是高度简化的版本:


const char * fname;

const char * execname =" / usr / bin / foo" ;;

char * pathname;

char * str;

...

strcpy(pathname,execname); < - 在我的平台上

我计划使用strlcpy():-)



无论你使用什么(我都不知道)什么strlcpy()应该这样做,

我希望在剩下的代码中为路径名分配足够的内存'

指向。 />


if((str = strrchr(pathname,''/''))!= NULL)

{

* ++ str =''\''';



我想你的意思是这里有


* str ++ =''\''';


因为我猜你打算覆盖''/''而不是'f''

跟随它。


鉴于execname指向的内容已被成功复制到

内存中指向'pathname'指向的内容,''str''现在指向

foo部分在内存中分配给''pathname'',其中包含


| ------这里的路径名点数

v

''/'','''','s'',''r'',''/'',''b'',''我',''n'', ''\''',''f'','''',''o'',''\ n''

^

str points here -------------- |


''str - pathname''是介于(9)之间的字符数。


fname = execname +(str - pathname); < - 某种程度上,fname



''fname''现在指向''execname + 9'',即
$ b中的第10个字符''execname''指向的$ b内存。并且,可能不再令人惊讶,

在这个位置开始foo该字符串的一部分。


最终成为foo按预期

}否则{

fname = execname;

* pathname =''\ 0'';

}



问候,Jensa

-

\ Jens Thoms Toerring ___ jt@toerring.de

\ __________________________ http://toerring.de


Hi all,

I have an elementary question, of which I never grasped, and i''d like
to finally put it to rest.

I have the following code, which although makes use of the string
library, and could therefore be labelled as OT, the actual question I
have relates to the code around it. This is the highly abridged
version:

const char *fname;
const char *execname="/usr/bin/foo";
char *pathname;
char *str;

....
strcpy(pathname,execname); <- on my platform
I plan to use strlcpy() :-)
if((str=strrchr(pathname,''/'')) != NULL)
{
*++str=''\0'';
fname=execname+(str - pathname); <- somehow, fname
ends up being "foo" as intended
} else {
fname=execname;
*pathname=''\0'';
}

I have checked the ascii values, for the above, and turning the
resulting ascii codes (which seem to be concat''d together), I do not
produce foo.

Would appreciate any help on this, as it''s frying my peonic brain!

thanks

KB.

解决方案

Krumble Bunk wrote:

I have an elementary question, of which I never grasped, and i''d like
to finally put it to rest.

(You never actually pose a question, but I assume it to be
"why does this work?")

I have the following code, which although makes use of the string
library, and could therefore be labelled as OT, the actual question I
have relates to the code around it. This is the highly abridged
version:

const char *fname;
const char *execname="/usr/bin/foo";
char *pathname;
char *str;

...
strcpy(pathname,execname); <- on my platform
I plan to use strlcpy() :-)
if((str=strrchr(pathname,''/'')) != NULL)
{
*++str=''\0'';
fname=execname+(str - pathname); <- somehow, fname
ends up being "foo" as intended

`str` and `pathname` are pointers. Subtraction of pointers
(that point into the same object) produces the difference
between them -- how much to add to the second to get the first.
It isn''t "string subtraction".

So after the test, if it succeeds, `str` points to the last `/`
in `pathname`. Then it gets incremented (and a 0 written over
the `/`, so that `pathname` points to the string up-to-but-not-
including the `/`, ie, "/usr/bin"). Now the difference between
`str` and `pathname` -- notice that `str` points into the same
object as `pathname` does -- is the number of characters before
the "foo" in the string. Adding that to `execname` gets a pointer
to the "foo" in execname''s "/usr/bin/foo".

} else {
fname=execname;
*pathname=''\0'';
}

I have checked the ascii values, for the above,

Why? The code works whatever the character set is. (It might not
be /useful/ on a machine with a different character set, but that''s
another matter.)

and turning the
resulting ascii codes (which seem to be concat''d together), I do not
produce foo.

Would appreciate any help on this, as it''s frying my peonic brain!

Ah, well, that''s the problem -- you should have asked for a
/positronic/ brain, not a /peonic/ one.

--
"Thereafter, events may roll unheeded." /Foundation/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN


On Apr 17, 3:41 pm, Chris Dollin <chris.dol...@hp.comwrote:

Krumble Bunk wrote:

I have an elementary question, of which I never grasped, and i''d like
to finally put it to rest.


(You never actually pose a question, but I assume it to be
"why does this work?")

I have the following code, which although makes use of the string
library, and could therefore be labelled as OT, the actual question I
have relates to the code around it. This is the highly abridged
version:

const char *fname;
const char *execname="/usr/bin/foo";
char *pathname;
char *str;

...
strcpy(pathname,execname); <- on my platform
I plan to use strlcpy() :-)
if((str=strrchr(pathname,''/'')) != NULL)
{
*++str=''\0'';
fname=execname+(str - pathname); <- somehow, fname
ends up being "foo" as intended


`str` and `pathname` are pointers. Subtraction of pointers
(that point into the same object) produces the difference
between them -- how much to add to the second to get the first.
It isn''t "string subtraction".

So after the test, if it succeeds, `str` points to the last `/`
in `pathname`. Then it gets incremented (and a 0 written over
the `/`, so that `pathname` points to the string up-to-but-not-
including the `/`, ie, "/usr/bin"). Now the difference between
`str` and `pathname` -- notice that `str` points into the same
object as `pathname` does -- is the number of characters before
the "foo" in the string. Adding that to `execname` gets a pointer
to the "foo" in execname''s "/usr/bin/foo".

} else {
fname=execname;
*pathname=''\0'';
}

I have checked the ascii values, for the above,


Why? The code works whatever the character set is. (It might not
be /useful/ on a machine with a different character set, but that''s
another matter.)

and turning the
resulting ascii codes (which seem to be concat''d together), I do not
produce foo.

Would appreciate any help on this, as it''s frying my peonic brain!


Ah, well, that''s the problem -- you should have asked for a
/positronic/ brain, not a /peonic/ one.

--
"Thereafter, events may roll unheeded." /Foundation/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Chris - many thanks for your help! Answers my question nicely.

cheers

KB.


Krumble Bunk <kr*********@gmail.comwrote:

I have the following code, which although makes use of the string
library, and could therefore be labelled as OT, the actual question I
have relates to the code around it.

<Reordered the paragraphs of the post a bit>

I have checked the ascii values, for the above, and turning the
resulting ascii codes (which seem to be concat''d together), I do not
produce foo.

Lookig at ASCII values won''t explain the mystery;-) You''re not
"subtracing strings" (whatever this might mean), you''re subtracting
pointers. This is allowed as long as the pointers involved point to
the same object (as they do here since they both point somewhere
into the string pointed to by ''pathname'') and then return the
number of elements between the two pointers.

This is the highly abridged version:

const char *fname;
const char *execname="/usr/bin/foo";
char *pathname;
char *str;
...
strcpy(pathname,execname); <- on my platform
I plan to use strlcpy() :-)

Whatever you use (I don''t know what strlcpy() is supposed to do),
I hope in the left-out code you allocate enough memory for ''pathname''
to point to.

if((str=strrchr(pathname,''/'')) != NULL)
{
*++str=''\0'';

I guess you meant here to have

*str++ = ''\0'';

since I guess you intended to overwrite the ''/'' and not the ''f''
following it.

Given that what ''execname'' points to has successfully been copied to
memory pointed to what ''pathname'' points to, ''str'' now points to
the "foo" part in the memory assigned to ''pathname'', which contains

|------ pathname points here
v
''/'', ''u'', ''s'', ''r'', ''/'', ''b'', ''i'', ''n'', ''\0'', ''f'', ''o'', ''o'', ''\n''
^
str points here --------------|

''str - pathname'' is then the number of characters in between (9).

fname=execname+(str - pathname); <- somehow, fname

''fname'' points now to ''execname + 9'', i.e. the 10th character in the
memory pointed to by ''execname''. And, probably not surprising anymore,
at this position starts the "foo" part of that string.

ends up being "foo" as intended
} else {
fname=execname;
*pathname=''\0'';
}

Regards, Jensa
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de


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

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