C 生成任意大小的字符串并附加其他字符串:如何生成?

C 生成任意大小的字符串并附加其他字符串:如何生成?,c,string,append,C,String,Append,对于未知大小的字符串的动态构造,我遇到了一些问题。为了解释我的问题,我编写了这个小代码(伪C代码),它应该在每次新字符串出现时生成一个finalstring: char *finalstring; while (/* condition */) { char *tmp = get_new_string(); finalstring = strcat(finalstring, tmp); } printf("%s\n", finalstring); 在每次迭代中,get\u ne

对于未知大小的字符串的动态构造,我遇到了一些问题。为了解释我的问题,我编写了这个小代码(伪C代码),它应该在每次新字符串出现时生成一个
finalstring

char *finalstring;
while (/* condition */) {
    char *tmp = get_new_string();
    finalstring = strcat(finalstring, tmp);
}
printf("%s\n", finalstring);
在每次迭代中,
get\u new\u string()
将得到一个不同的字符串,我想将其添加到
finalstring
。每个字符串始终是固定的
STRSIZE
,因此在每次迭代
finalstring
时,可以增加
STRSIZE
char
s


如何在C中真正实现这一点?我不确定我应该如何使用
malloc
s…

您可以
重新分配(使用
realloc
)到
oldsize+新字符串长度
,以获得可以连接新字符串的变量。那将为你解决问题

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (void) {
    char *finalstring = NULL;
    size_t oldsize = 0;
    size_t i=0;
    char s[][10]={"AA","BB","CC","DD"};
    size_t sz = sizeof s / sizeof s[0];
    while (i < sz) {
        size_t newsize = strlen(s[i]);
        size_t templen = newsize + oldsize ;
        if(oldsize == 0) templen++;
        char *temp  = realloc(finalstring , templen );
        if(temp == NULL){
            perror("realloc failed");
            exit(EXIT_FAILURE);
        }
        finalstring = temp;

        if(oldsize == 0){
            strcpy(finalstring,s[i]);
        }
        else
            strcat(finalstring,s[i]);
        oldsize = z;
        i++;
    }

   printf("%s\n",finalstring );
   free(finalstring);
   return 0;
}
#包括
#包括
#包括
内部主(空){
char*finalstring=NULL;
大小\u t旧大小=0;
尺寸i=0;
字符s[][10]={“AA”、“BB”、“CC”、“DD”};
size_t sz=sizeof s/sizeof s[0];
而(i

对于第一个字符串,将再分配一个空间以保留
\0
。连续的字符串不需要这样做,因为前面的
\0
将被覆盖。您将使用从
get\u new\u string()
函数返回的字符串,而不是
s[i]

您可以
重新分配到
旧大小+新字符串长度
,以获得可以连接新字符串的变量。那将为你解决问题

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (void) {
    char *finalstring = NULL;
    size_t oldsize = 0;
    size_t i=0;
    char s[][10]={"AA","BB","CC","DD"};
    size_t sz = sizeof s / sizeof s[0];
    while (i < sz) {
        size_t newsize = strlen(s[i]);
        size_t templen = newsize + oldsize ;
        if(oldsize == 0) templen++;
        char *temp  = realloc(finalstring , templen );
        if(temp == NULL){
            perror("realloc failed");
            exit(EXIT_FAILURE);
        }
        finalstring = temp;

        if(oldsize == 0){
            strcpy(finalstring,s[i]);
        }
        else
            strcat(finalstring,s[i]);
        oldsize = z;
        i++;
    }

   printf("%s\n",finalstring );
   free(finalstring);
   return 0;
}
#包括
#包括
#包括
内部主(空){
char*finalstring=NULL;
大小\u t旧大小=0;
尺寸i=0;
字符s[][10]={“AA”、“BB”、“CC”、“DD”};
size_t sz=sizeof s/sizeof s[0];
而(i
对于第一个字符串,将再分配一个空间以保留
\0
。连续的字符串不需要这样做,因为前面的
\0
将被覆盖。您将使用从
get\u new\u string()
函数返回的字符串,而不是
s[i]

生成任意大小的字符串并附加其他字符串:如何生成

跟踪字符串长度,并根据需要为新字符串重新分配

char *final_string = NULL;
size_t final_len = 0;  // size_t is the best type for "size" math and array indexing
while (/* condition */) {
    char *tmp = get_new_string();
    size_t tmp_len = strlen(tmp);

    // Allocate enough for the length of both parts and a \0   
    char *s = realloc(final_string, final_len + tmp_len + 1);
    if (s == NULL) {
      exit(EXIT_FAILURE);
    }
    final_string = s; 

    // As code knows the strings lengths, 
    // easy enough to use fast memcpy to concatenate....
    memcpy(final_string + final_len, tmp, tmp_len + 1);
    final_len += tmp_len;
}

if (final_string) {  // If needed in case loop body never executed.
  printf("%s\n", final_string);
}
free(final_string);

这个答案并没有利用“每个字符串将始终是固定的
STRSIZE
”。这是一个略微有利的条件

生成任意大小的字符串并附加其他字符串:如何生成

跟踪字符串长度,并根据需要为新字符串重新分配

char *final_string = NULL;
size_t final_len = 0;  // size_t is the best type for "size" math and array indexing
while (/* condition */) {
    char *tmp = get_new_string();
    size_t tmp_len = strlen(tmp);

    // Allocate enough for the length of both parts and a \0   
    char *s = realloc(final_string, final_len + tmp_len + 1);
    if (s == NULL) {
      exit(EXIT_FAILURE);
    }
    final_string = s; 

    // As code knows the strings lengths, 
    // easy enough to use fast memcpy to concatenate....
    memcpy(final_string + final_len, tmp, tmp_len + 1);
    final_len += tmp_len;
}

if (final_string) {  // If needed in case loop body never executed.
  printf("%s\n", final_string);
}
free(final_string);

这个答案并没有利用“每个字符串将始终是固定的
STRSIZE
”。这是一个略微有利的条件