Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不使用malloc重复不同的字符串模式_C_String_Repeat - Fatal编程技术网

不使用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*/ }