strcat函数的问题 [英] problem with strcat function

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

问题描述



i已经编写了这个strcat,但有时会出现问题,而

包含一些包含

二进制的字符串,如果包含字符串零,

功能,它取字符串1及其长度string2及其长度为

参数


请告诉纠正,


这里iam使用

typedef char SINT8;

typedef unsigned char UINT8;

typedef short SINT16 ;

typedef unsigned short UINT16;

typedef int SINT32;

typedef unsigned int UINT32;

typedef long long UINT64;


UINT8 *

Stringcat(UINT8 * str1

,UINT32 str1len

,UINT8 * str2

,UINT32 str2len)

{

UINT8 * str3,* temp;

temp = str3 = (char *)calloc((str1len + str2len)+ 1),1);

while((* str3 ++ = * str1 ++));

str3 - ;

whi le((* str3 ++ = * str2 ++));

返回temp;

}

问候

ramaswamy BM

banglore


i have written this strcat but sometime it is giving problem, while
handeling some strings containing
binary and if string containing zero ,
funtion which takes string 1 and its length string2 and its length as
arguments

please tell the correction ,

here iam using
typedef char SINT8;
typedef unsigned char UINT8;
typedef short SINT16;
typedef unsigned short UINT16;
typedef int SINT32;
typedef unsigned int UINT32;
typedef long long UINT64;

UINT8 *
Stringcat (UINT8 *str1
, UINT32 str1len
, UINT8 *str2
, UINT32 str2len )
{
UINT8 *str3, *temp;
temp = str3 = (char *) calloc (( str1len + str2len) + 1) ,1 );
while ( ( *str3++ = *str1++) );
str3--;
while ( ( *str3++ = *str2++ ) );
return temp;
}
regards
ramaswamy BM
banglore

推荐答案


ra ****** @ gmail.com 写道:

我写了这个strcat


标准版有什么问题?如果你需要第三个字符串

包含结果,那么使用`strcat()`和一些

附加代码应该很容易。

但是有时它会给出问题,而
handeling包含
二进制的字符串,如果字符串包含零,


我真的不明白你是什么用二进制表示,但是你应该知道C字符串是由`char`终止的`char`数组,其中

的值为零。

功能,它取字符串1及其长度string2及其长度作为


请告诉纠正,

这里我使用
typedef char SINT8;
typedef unsigned char UINT8;
typedef short SINT16;
typedef unsigned short UINT16;
typedef int SINT32;
typedef unsigned int UINT32;
typedef long long UINT64;


这些都是极具误导性的。当您的基础架构发生变化时,会发生什么(对于读取代码的人)

以及上面隐含的尺寸



UINT8 *
Stringcat(UINT8 * str1
,UINT32 str1len
,UINT8 * str2
,UINT32 str2len)
{
UINT8 * str3,* temp;
temp = str3 =(char *)calloc((str1len + str2len)+ 1),1);


这里不需要演员。它可能掩盖了遗漏,包括< stdlib.h>。

while((* str3 ++ = * str1 ++));


这里有你的零。问题。你复制,直到遇到零。

首先传递字符串长度是什么意思?

str3--;
while((* str3 ++) = * str2 ++));


同样在这里...


你可能想要从字符串的开头到结尾的简单循环,

使用你传递的长度。

返回温度;
}

i have written this strcat
What''s wrong with the standard one? If you need a third string to
contain the result, it should be easy enough to use `strcat()` and some
additional code.
but sometime it is giving problem, while
handeling some strings containing
binary and if string containing zero ,
I don''t really understand what you mean by "binary", but you should
know that C strings are arrays of `char` terminated by the `char` with
the value of zero.
funtion which takes string 1 and its length string2 and its length as
arguments

please tell the correction ,

here iam using
typedef char SINT8;
typedef unsigned char UINT8;
typedef short SINT16;
typedef unsigned short UINT16;
typedef int SINT32;
typedef unsigned int UINT32;
typedef long long UINT64;
These are highly misleading. What happens (to someone reading the code)
when your underlying architecture changes, together with the sizes
implied above?
UINT8 *
Stringcat (UINT8 *str1
, UINT32 str1len
, UINT8 *str2
, UINT32 str2len )
{
UINT8 *str3, *temp;
temp = str3 = (char *) calloc (( str1len + str2len) + 1) ,1 );
No need for a cast here. It may mask omission to include <stdlib.h>.
while ( ( *str3++ = *str1++) );
Herein lies your "zero" problem. You copy until you encounter zero.
What was then the point of passing string lengths in the first place?
str3--;
while ( ( *str3++ = *str2++ ) );
Same here...

You probably want simple loops from start to the end of the string,
using the lengths that you get passed.
return temp;
}




我仍​​然建议包装标准` strcat()`一些额外的代码。


-

BR,弗拉基米尔



I''d still recommend wrapping standard `strcat()` in some extra code.

--
BR, Vladimir




Vladimir S. Oka写道:

Vladimir S. Oka wrote:
ra **** **@gmail.com 写道:

我写过这个strcat

i have written this strcat



这有什么问题标准的一个?如果你需要第三个字符串来包含结果,那么使用`strcat()`和一些附加代码应该很容易。



What''s wrong with the standard one? If you need a third string to
contain the result, it should be easy enough to use `strcat()` and some
additional code.

但是有时候它给出了问题,而
handeling包含
二进制的字符串,如果字符串包含零,
but sometime it is giving problem, while
handeling some strings containing
binary and if string containing zero ,



我真的不明白你的意思是什么?二进制,但你应该知道C字符串是由`char`终止的`char`数组,其值为零。



I don''t really understand what you mean by "binary", but you should
know that C strings are arrays of `char` terminated by the `char` with
the value of zero.

funtion哪个取字符串1及其长度string2及其长度作为


请告诉纠正,

这里我使用
typedef char SINT8;
typedef unsigned char UINT8;
typedef short SINT16;
typedef unsigned short UINT16;
typedef int SINT32;
typedef unsigned int UINT32;
typedef long long UINT64;
funtion which takes string 1 and its length string2 and its length as
arguments

please tell the correction ,

here iam using
typedef char SINT8;
typedef unsigned char UINT8;
typedef short SINT16;
typedef unsigned short UINT16;
typedef int SINT32;
typedef unsigned int UINT32;
typedef long long UINT64;



这些都很高hly误导。当您的基础架构发生变化时,会发生什么?(对于阅读代码的人)
以及上面隐含的大小?



These are highly misleading. What happens (to someone reading the code)
when your underlying architecture changes, together with the sizes
implied above?

UINT8 *
Stringcat( UINT8 * str1
,UINT32 str1len
,UINT8 * str2
,UINT32 str2len)
{
UINT8 * str3,* temp;
temp = str3 = (char *)calloc((str1len + str2len)+ 1),1);
UINT8 *
Stringcat (UINT8 *str1
, UINT32 str1len
, UINT8 *str2
, UINT32 str2len )
{
UINT8 *str3, *temp;
temp = str3 = (char *) calloc (( str1len + str2len) + 1) ,1 );



这里不需要演员。它可能会掩盖遗漏,包括< stdlib.h>。



No need for a cast here. It may mask omission to include <stdlib.h>.




您还应该检查是否成功。 `calloc()`,就像`malloc()`

如果分配不成功则返回NULL。



You should also check for success. `calloc()`, just like `malloc()`
returns NULL if allocation did not succeed.

while ((* str3 ++ = * str1 ++));
while ( ( *str3++ = *str1++) );



这里有你的零。问题。你复制,直到你遇到零。
首先传递字符串长度是什么意思?



Herein lies your "zero" problem. You copy until you encounter zero.
What was then the point of passing string lengths in the first place?

str3--;
while((* str3 ++) = * str2 ++));
str3--;
while ( ( *str3++ = *str2++ ) );



同样在这里......

你可能想要从字符串的开头到结尾的简单循环,
使用你通过的长度。



Same here...

You probably want simple loops from start to the end of the string,
using the lengths that you get passed.

return temp;
}
return temp;
}



我仍然建议包装标准的`strcat( )``一些额外的代码。

-
BR,Vladimir



I''d still recommend wrapping standard `strcat()` in some extra code.

--
BR, Vladimir






ra ****** @ gmail.com 写道:
我写过这个strcat但有时它提出问题,而
handeling包含
二进制的字符串,如果字符串包含零,
功能,其中字符串1及其长度为string2,其长度为
参数

请告诉更正,

这里我使用
typedef char SINT8;
typedef unsigned char UINT8;
typedef short SINT16;
typedef unsigned short UINT16;
typedef int SINT32;
typedef unsigned int UINT32;
typedef long long UINT64;


这些typedef可能具有误导性。

预定义类型的大小可以并且确实因实现而异。普通字符

可以是签名的也可以是未签名的;如果你想签名类型使用

" signed char" ;.


很幸运你不使用UINT64,因为你'已经将它定义为

*签名*类型。


了解您正在使用的类型的确切大小通常不是

你可能会认为很有用。更常见的是,你应该根据它们的用途来选择类型,而不是它们的大小。

UINT8 *
Stringcat( UINT8 * str1
,UINT32 str1len
,UINT8 * str2
,UINT32 str2len)
{
UINT8 * str3,* temp;
temp = str3 = (char *)calloc((str1len + str2len)+ 1),1);
while((* str3 ++ = * str1 ++));
str3--;
while((* str3 ++) = * str2 ++));
return temp;
}
i have written this strcat but sometime it is giving problem, while
handeling some strings containing
binary and if string containing zero ,
funtion which takes string 1 and its length string2 and its length as
arguments

please tell the correction ,

here iam using
typedef char SINT8;
typedef unsigned char UINT8;
typedef short SINT16;
typedef unsigned short UINT16;
typedef int SINT32;
typedef unsigned int UINT32;
typedef long long UINT64;
These typedefs are potentially misleading. The sizes of the
predefined types can and do vary across implementations. Plain char
can be either signed or unsigned; if you want a signed type use
"signed char".

It''s fortunate that you don''t use UINT64, since you''ve defined it as a
*signed* type.

Knowing the exact sizes of the types you''re using is usually not as
useful as you might think. More often, you should select types based
on what they''re to be used for rather than on how big they are.
UINT8 *
Stringcat (UINT8 *str1
, UINT32 str1len
, UINT8 *str2
, UINT32 str2len )
{
UINT8 *str3, *temp;
temp = str3 = (char *) calloc (( str1len + str2len) + 1) ,1 );
while ( ( *str3++ = *str1++) );
str3--;
while ( ( *str3++ = *str2++ ) );
return temp;
}




字符串是一个char数组。只需使用类型字符比UINT8更好。


使用size_t(在< stddef.h>中定义)的大小和长度。这个

消除了对任何typedef的需求。


你的函数与strcat()完全不同(strcat()

没有分配任何内存)。这不是一件坏事,但名字

有点误导。


-

Keith Thompson( The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



A string is an array of char. Just use type char rahter than UINT8.

Use size_t (defined in <stddef.h>) for sizes and lengths. This
eliminates the need for any of your typedefs.

Your function does something quite different from strcat() (strcat()
doesn''t allocated any memory). That''s not a bad thing, but the name
is a bit misleading.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


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

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