字符串移动宏。 [英] String move Macro.

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

问题描述

大家好,

下面的宏从指定的索引中移动指定的字节数

source

字符串到指定的目标索引字符串。


#define STRMOV(src_str,src_ind,length,dst_str,dst_ind)\

do \

{ \

int s,d; \

s = src_ind; \

d = dst_ind; \

while(s< length&& src_str [s]!=''\ 0'')\

{\

dst_str [d] = src_str [s]; \

d ++; \

s ++; \

} \

} while(0);


我已经测试了一些输入。看起来这是执行

预期功能。

如果对此进行任何更正或任何改进,请告知我们

宏。


谢谢

Hi all,
Below macro moves specified number of bytes from a specified index of
source
string to a specified index of destination string.

#define STRMOV(src_str,src_ind,length,dst_str,dst_ind)\
do\
{\
int s,d;\
s = src_ind;\
d = dst_ind;\
while(s < length && src_str[s] != ''\0'')\
{\
dst_str[d] = src_str[s];\
d++;\
s++;\
}\
}while(0);

I have tested this for some inputs. Looks like this is performing the
intended functionality.
Please let me know if there are any corrections or any improvements to
be done to this macro.

Thanks

推荐答案


va ****** @ rediffmail.com 写道:
大家好,
下面的宏移动指定从指定的源字符串索引到指定的目标字符串索引的字节数。

#define STRMOV(src_str,src_ind,length,dst_str,dst_ind)

不清楚这个(或者更确切地说,它是误导性的)或上面的

描述,'length`指的是源字符串的长度,

而不是要复制的子字符串。如果您提供指定数量的

字节,什么都没发生。如果你使用`strlen()`提供长度,

编译器(在偏执警告级别)抱怨comaprison

签名和未签名的值。

do \
{\
int s,d; \
s = src_ind; \
d = dst_ind;

为什么你需要两者?你甚至不使用`d`。

while(s< length&& src_str [s]!=''\ 0'')\
{ \
dst_str [d] = src_str [s]; \
d ++; \
s ++; \
} \
} while(0) ;

我已经测试了一些输入。看起来这正在执行
预期的功能。
如果对此宏进行任何更正或任何改进,请告诉我。
Hi all,
Below macro moves specified number of bytes from a specified index of
source string to a specified index of destination string.

#define STRMOV(src_str,src_ind,length,dst_str,dst_ind)
It is not clear from this (or rather, it''s misleading) or your
description above, that `length` refers to the length of source string,
and not the substring to copy. If you supply the "specified number of
bytes" nothing happens. If you use `strlen()` to supply the length,
compiler (at the paranoid warning level) complains about comaprison
betwee signed and unsigned values.
do\
{\
int s,d;\
s = src_ind;\
d = dst_ind;
Why do you need both? You don''t even use `d`.
while(s < length && src_str[s] != ''\0'')\
{\
dst_str[d] = src_str[s];\
d++;\
s++;\
}\
}while(0);

I have tested this for some inputs. Looks like this is performing the
intended functionality.
Please let me know if there are any corrections or any improvements to
be done to this macro.




使它成为一个合适的功能。


你认为你在性能方面获得了什么,你在安全方面失败,

可读性和可维护性。例如。如果你错误地提供了一个

`int`作为源字符串(拼写错误,拼写错误),错误消息不是很有用:
$ b / b


main.c:27:错误:下标值既不是数组也不是指针

main.c:27:错误:下标值既不是数组也不是指针


更不用说错误指向的行是宏使用的是
,而不是宏中失败的行。


作为一个函数,你将获得

参数的免费类型编译时类型检查,以及更多......



Make it a proper function.

What you think you gain in performance, you lose in safety,
readability, and maintainability. E.g. if you erroneously supply an
`int` as the source string (typos, typos), the error message is less
than useful:

main.c:27: error: subscripted value is neither array nor pointer
main.c:27: error: subscripted value is neither array nor pointer

Not to mention that the line the error points to is where the macro was
used, rather than the line inside the macro that failed.

As a function, you''d get free type compile time type checking of
parameters, and much, much more...


va******@rediffmail.com 写道:
大家好,
下面的宏将指定的字节数从指定的
source
字符串移动到指定的目标字符串索引。

#define STRMOV(src_str,src_ind,length,dst_str,dst_ind)\
do \
{\
int s,d; \
s = src_ind; \
d = dst_ind; \
while(s<长度&& src_str [s]!=''\ 0'')\
{\
dst_str [d] = src_str [s]; \
d ++; \
s ++; \
} \
} while(0);
Hi all,
Below macro moves specified number of bytes from a specified index of
source
string to a specified index of destination string.

#define STRMOV(src_str,src_ind,length,dst_str,dst_ind)\
do\
{\
int s,d;\
s = src_ind;\
d = dst_ind;\
while(s < length && src_str[s] != ''\0'')\
{\
dst_str[d] = src_str[s];\
d++;\
s++;\
}\
}while(0);




#include< string.h>


#define STRMOV(src_str,src_ind,length,dst_str,dst_ind)\

do {

strncpy(src_str [src_ind],

dst_str [dst_ind],

长度);

}而(0)


.. ..或者只是strncpy()。或者你不允许使用strncpy?



#include <string.h>

#define STRMOV(src_str,src_ind,length,dst_str,dst_ind) \
do {
strncpy( src_str[ src_ind ],
dst_str[ dst_ind ],
length );
} while( 0 )

.... or just strncpy(). Or are you not allowed to use strncpy ?



va ****** @ rediffmail.com 写道:
#define STRMOV(src_str,src_ind,length,dst_str,dst_ind)\
#define STRMOV(src_str,src_ind,length,dst_str,dst_ind)\



我这样做的原因是为了提高执行速度(要求
来自客户的
)。目前我正在使用memcpy来实现这一目标。现在我

计划编写我自己的宏。


The reason I am doing this is to improve execution speed(Requirement
from client). Currently I am using memcpy to achieve this. Now I am
planning to write my own Macro.


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

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