strcat strncat和strlen [英] strcat strncat and strlen

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

问题描述

Strncat应该比strcat好,因为某些原因我已经阅读了b $ b。这是因为潜在的缓冲区溢出?我已经正确编译了

并使用了strlen,我只是想知道需要返回一个

strlen?


已经无论如何,任何人都使用了strlen功能吗?


比尔

Strncat is supposed to be better than strcat for some reason I''ve
read. Is this because of a potential buffer overflow? I have compiled
properly and used strlen too and I just wonder what is the need to return a
strlen?

Has anyone used quite abit anyway the strlen function?

Bill

推荐答案

Bill Cunningham写道:
Bill Cunningham wrote:

由于某种原因,Strncat应该比strcat更好

我已读过。
Strncat is supposed to be better than strcat for some reason
I''ve read.



它的名称与它的名称有所不同。我们已经有了无数的线程来处理这个话题。只需进行谷歌搜索。

It does something different to what it''s name would suggest. We have had
innumerable threads dealing with this topic. Just do a Google search.


这是因为潜在的缓冲区溢出?
Is this because of a potential buffer overflow?



嗯,它可以用于那个目的,在这种情况下,它会留下一个

char的数组而不是目的地中的字符串。

Well, it can be used with that aim, in which case it leaves an array of
char instead of a string in the destination.


我已经正确编译并使用了strlen,我只是想知道什么是

需要返回一个strlen?


有没有人使用过strite函数?
I have compiled properly and used strlen too and I just wonder what is
the need to return a strlen?

Has anyone used quite abit anyway the strlen function?



你没有任何意义。你在问为什么strlen存在?

应该很明显,即使对你来说也是如此。你怎么找到一个

零终止字符串的长度?

You are not making any sense. Are you asking why strlen exists? It
should be obvious, even to you. How else do you find the length of a
zero terminated string?


你没有任何意义。你在问为什么strlen存在?它
You are not making any sense. Are you asking why strlen exists? It

应该是显而易见的,即使对你来说也是如此。你怎么找到一个

零终止字符串的长度?
should be obvious, even to you. How else do you find the length of a
zero terminated string?



C不得将''\0''计为字符串的一部分。这是简单的代码

我试过了,


int main(无效){

size_t t;

char hello [] =" hello world \ n" ;;

t = strlen(hello);

printf("%i",hello);

}

返回的整数是12.当我从字符串中删除''\ n''时,

并重新编译11是返回的数字。

为什么在生产代码中有人想知道

字符串的长度?这就是我的要求。因此,我缺乏经验说明了自己。

C must not count ''\0'' as being part of a string. This is the simple code
I tried,

int main(void) {
size_t t;
char hello[]="hello world\n";
t=strlen(hello);
printf("%i",hello);
}
The integer returned was 12. When I removed the ''\n'' from the string
and recompiled 11 was the number returned.
Why in production code would someone want to know the length of a
string? That''s what I am asking. Hence my inexperience speaks for itself.


Bill Cunningham写道:
Bill Cunningham wrote:

>你没有任何意义。你在问为什么strlen存在?它应该是显而易见的,即使对你来说也是如此。你怎么找到一个
零终止字符串的长度?
>You are not making any sense. Are you asking why strlen exists? It
should be obvious, even to you. How else do you find the length of a
zero terminated string?



C不得将''\ 0''计为字符串的一部分。


C must not count ''\0'' as being part of a string.



它是C字符串的一部分。但是strlen不计算它。

It is a part of a C string. However it is not counted by strlen.


这是我试过的简单代码,


int main(void){

size_t t;

char hello [] =" hello world \ n";

t = strlen(hello);

printf("%i",hello);
This is the simple code I tried,

int main(void) {
size_t t;
char hello[]="hello world\n";
t=strlen(hello);
printf("%i",hello);



size_t的格式为%zu。如果你的编译器不支持这个

那么下一个最好的方法是使用%lu并将它的参数转换为

unsigned long。

The format for size_t is %zu. If your compiler does not support this
then the next best method is to use %lu and cast it''s argument to
unsigned long.


}

返回的整数是12.当我从

字符串中删除''\ n''并重新编译11是返回的数字。
}
The integer returned was 12. When I removed the ''\n'' from the
string and recompiled 11 was the number returned.



是的。那么?

Yes. So?


为什么在生产代码中有人想知道

字符串的长度?这就是我的要求。因此,我的经验不足说明

本身。
Why in production code would someone want to know the length of a
string? That''s what I am asking. Hence my inexperience speaks for
itself.



并非所有情况都是这样的,我们可以在

编译时知道字符串的长度,如上例所示。通常在运行时构造字符串

,从外部文件或用户和许多程序接收

进行大量字符串处理,如拆分,连接,

等。即使您的代码知道字符串的长度,其他

库代码也不会。让他们在你的琴弦上采取合理行动的一种方式

是使用strlen来获取他们的长度,或者在他们处理它时注意null

角色。


例如在程序中:


int main(int argc,char ** argv){

/ *。 .. * /

}


如果程序给出了任何命令行参数,那么就没有

方式了程序除了使用

strlen(或等效的内联代码)之外,还要找出它们的长度。


因此,对于C字符串来说,strlen是必要的,因为它们不会与他们一起带着他们的
长度。然而,好的程序试图通过缓存以前的结果和存储已知的

字符串的长度来最小化

strlen的使用,而不是丢弃信息并重新计算每个

到处。

Not all situations are such that we can know the length of a string at
compile time, as in your example above. Often strings are constructed
at runtime, received from external files or the user and many programs
do quite a lot of string processing, like splitting, concatenating,
etc. Also even if your code knows the lengths of your strings, other
library code will not. One way for them to act sanely upon your strings
is to use strlen to get their length, or watch out for the null
character as they process it.

For example in the program:

int main(int argc, char **argv) {
/* ... */
}

if the program is given any command line parameters, then there is no
way for the program to find out their lengths other than by using
strlen (or equivalent inline code).

So strlen is necessary with C strings because they don''t carry their
length with them. However good programs try to minimise the use of
strlen by caching previous results and storing the lengths of known
strings, instead of discarding the information and recomputing it every
here and there.


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

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