不使用malloc重复不同的字符串模式
我的重复方法有问题不使用malloc重复不同的字符串模式,c,string,repeat,C,String,Repeat,我的重复方法有问题 repeat("11111", 10); repeat("22222", 10); 应产生输出1111111(10x1)和22222(10x2) 但是实际输出都是1111111(10x1),所以我的缓冲区还是一样的 这是我的密码: static char buffer[PAYLOAD_SIZE]; static void repeat(char *pattern, int size) { int pattern_size = strlen(pattern);
repeat("11111", 10);
repeat("22222", 10);
应产生输出1111111(10x1)和22222(10x2)
但是实际输出都是1111111(10x1),所以我的缓冲区还是一样的
这是我的密码:
static char buffer[PAYLOAD_SIZE];
static void repeat(char *pattern, int size) {
int pattern_size = strlen(pattern);
int n = size / pattern_size; //quotient of division
int rem = size % pattern_size; //remainder of division
int i; char *p;
for (i=0, p = buffer; i < n; ++i, p += pattern_size ) {
memcpy(p, pattern, pattern_size);
}
memcpy(p, pattern, rem);
}
它们可能都有不同的长度
您好,
Jan这似乎对其他人有用,所以这个问题可以结束了,我想。。。我必须找出问题所在……Hmmm,我无法重现你的问题。我定义了五种模式,对它们运行
repeat
,然后打印出buffer
。代码是正常的,除非调用代码中有错误。(您不能在缓冲区中以null终止重复的字符串,但我不知道这是否是出于设计。)是的,这是有意的。但是如果它对你有效,那么我想我必须深入研究其他代码。感谢验证。建议使用非重复模式来帮助调试,如“12345”。显示模式及其使用方式。缓冲区[PAYLOAD_SIZE]代码>需要内存管理。,
char *patterns[5] = { /*5 different patterns*/ }