Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.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
C 将字符与字符合并*_C - Fatal编程技术网

C 将字符与字符合并*

C 将字符与字符合并*,c,C,我正在尝试编写一个函数,它将获取char,然后生成char*,这是在堆中分配的。我该怎么做? 注意:下面的代码是不工作的,你能帮助修复吗 Ex: char* foo ( char x, char * xc ) { xc = realloc ( xc, 1 + strlen ( xc ) ) ; strcat ( xc, x ) ; return xc ; } p = h

我正在尝试编写一个函数,它将获取char,然后生成char*,这是在堆中分配的。我该怎么做? 注意:下面的代码是不工作的,你能帮助修复吗

Ex:

    char* foo ( char x, char * xc ) {


        xc =  realloc ( xc, 1 + strlen ( xc ) ) ;
        strcat ( xc, x ) ;

    return xc ;
    }

                             p =  heap variable
   foo ( 'a', NULL ) ==>     ------------
                             | 'a'| '\0'|
                             ------------

   foo ( 'b', p )    ===>    --------------------
                             | 'a' | 'b' | '\0' |
                             --------------------

   foo ( 'c', p )    ===>    --------------------------
                             | 'a' | 'b' | 'c' | '\0' |
                             --------------------------
接受两个指针。在您的示例中,您将传递一个字符作为第二个参数

使用函数族将字符连接到字符串

差不多

len =strlen(xc);
xc =  realloc ( xc, 2 + strlen ( xc ) ) ; //One for NULL character

sprintf(xc+len,"%c", x);
接受两个指针。在您的示例中,您将传递一个字符作为第二个参数

使用函数族将字符连接到字符串

差不多

len =strlen(xc);
xc =  realloc ( xc, 2 + strlen ( xc ) ) ; //One for NULL character

sprintf(xc+len,"%c", x);

NULL
不是字符串,因此您不能对其调用
strlen

size_t len = xc != NULL ? strlen(xc) : 0;
xc = realloc(xc, len + 1 + 1);
xc[len] = c;
xc[len + 1] = '\0';

NULL
不是字符串,因此您不能对其调用
strlen

size_t len = xc != NULL ? strlen(xc) : 0;
xc = realloc(xc, len + 1 + 1);
xc[len] = c;
xc[len + 1] = '\0';
有两个问题

  • 如果您的指针为空,您将在此地址上调用strlen
  • 您分配的内存不足
有两个问题

  • 如果您的指针为空,您将在此地址上调用strlen
  • 您分配的内存不足
试试这个:

char* append_char(char c, char * str)
{
    char* new_str;

    if(NULL != str) {
        new_str = realloc(str, strlen(str) + 2);
        strncpy(new_str, str, strlen(str));
        new_str[strlen(str)] = c;
        new_str[strlen(str)+1] = '\0';
    } else {
        new_str = malloc(2);
        new_str[0] = c;
        new_str[1] = '\0';
    }

    return new_str;
}
试试这个:

char* append_char(char c, char * str)
{
    char* new_str;

    if(NULL != str) {
        new_str = realloc(str, strlen(str) + 2);
        strncpy(new_str, str, strlen(str));
        new_str[strlen(str)] = c;
        new_str[strlen(str)+1] = '\0';
    } else {
        new_str = malloc(2);
        new_str[0] = c;
        new_str[1] = '\0';
    }

    return new_str;
}

我可以分享一点素描

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

int sl = 0; // sl for strlen(s), allocated memory size will be +1 byte
char* s = NULL;

char* ccat(char* s, char c) {
    if (s == NULL) sl = 1; // allocating new memory
    else           sl = strlen(s) + 1; // +1 for new char
    // realloc will work like malloc if s is NULL
    s = realloc(s, sl+1); // +1 for zero at the end of string
    if(s == NULL) {
        fprintf(stderr, "realloc error");
        exit(1);
    }
    s[sl-1] = c;
    s[sl] = '\0';
    return s;
}

int main(int argc, char** argv) {
    s = ccat(s, 'Y');
    printf("sl=%d s=%s\n", sl, s);

    s = ccat(s, 'P');
    printf("sl=%d s=%s\n", sl, s);

    s = ccat(s, 'A');
    printf("sl=%d s=%s\n", sl, s);

    if (s != NULL)
        free(s);
    exit(0);
}
#包括
#包括
#包括
int sl=0;//对于strlen,分配的内存大小为+1字节
char*s=NULL;
字符*ccat(字符*s,字符c){
如果(s==NULL)sl=1;//分配新内存
else sl=strlen(s)+1;对于新字符为//+1
//如果s为NULL,realloc将像malloc一样工作
s=realloc(s,sl+1);//+1表示字符串末尾的零
如果(s==NULL){
fprintf(标准“realloc错误”);
出口(1);
}
s[sl-1]=c;
s[sl]='\0';
返回s;
}
int main(int argc,字符**argv){
s=ccat(s,‘Y’);
printf(“sl=%d s=%s\n”,sl,s);
s=ccat(s,‘P’);
printf(“sl=%d s=%s\n”,sl,s);
s=ccat(s,‘A’);
printf(“sl=%d s=%s\n”,sl,s);
如果(s!=NULL)
免费的;
出口(0);
}

我可以分享一个小草图

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

int sl = 0; // sl for strlen(s), allocated memory size will be +1 byte
char* s = NULL;

char* ccat(char* s, char c) {
    if (s == NULL) sl = 1; // allocating new memory
    else           sl = strlen(s) + 1; // +1 for new char
    // realloc will work like malloc if s is NULL
    s = realloc(s, sl+1); // +1 for zero at the end of string
    if(s == NULL) {
        fprintf(stderr, "realloc error");
        exit(1);
    }
    s[sl-1] = c;
    s[sl] = '\0';
    return s;
}

int main(int argc, char** argv) {
    s = ccat(s, 'Y');
    printf("sl=%d s=%s\n", sl, s);

    s = ccat(s, 'P');
    printf("sl=%d s=%s\n", sl, s);

    s = ccat(s, 'A');
    printf("sl=%d s=%s\n", sl, s);

    if (s != NULL)
        free(s);
    exit(0);
}
#包括
#包括
#包括
int sl=0;//对于strlen,分配的内存大小为+1字节
char*s=NULL;
字符*ccat(字符*s,字符c){
如果(s==NULL)sl=1;//分配新内存
else sl=strlen(s)+1;对于新字符为//+1
//如果s为NULL,realloc将像malloc一样工作
s=realloc(s,sl+1);//+1表示字符串末尾的零
如果(s==NULL){
fprintf(标准“realloc错误”);
出口(1);
}
s[sl-1]=c;
s[sl]='\0';
返回s;
}
int main(int argc,字符**argv){
s=ccat(s,‘Y’);
printf(“sl=%d s=%s\n”,sl,s);
s=ccat(s,‘P’);
printf(“sl=%d s=%s\n”,sl,s);
s=ccat(s,‘A’);
printf(“sl=%d s=%s\n”,sl,s);
如果(s!=NULL)
免费的;
出口(0);
}

请记住,
1+strlen(xc)
只为字符串
xc
留出了足够的空间-仍然没有足够的空间容纳孤独的
字符x
。这是家庭作业吗?如果是,请标记。请记住,
1+strlen(xc)
只为字符串
xc
留出了足够的空间-仍然没有足够的空间容纳孤独的
字符x
。这是家庭作业吗?如果是,请标记。请注意,
sprintf()
和friends不允许使用字符串作为目标缓冲区和要格式化到目标中的参数。您不需要像
printf
家族函数那样沉重的东西。您正在realloc'd内存的末尾添加一个字符。只需执行
inti=strlen(xc);xc[i]=c;xc[i+1]=0。刚才看到罗兰的回答,这基本上就是我的意思。请注意,sprintf()和朋友们不允许使用字符串作为目标缓冲区和要格式化到目标中的参数。你不需要像printf家族函数那样沉重的东西。您正在realloc'd内存的末尾添加一个字符。只需执行
inti=strlen(xc);xc[i]=c;xc[i+1]=0。我刚看到罗兰的回答,基本上就是这个意思。不,你完全正确。现在检查我的编辑。应该处理好不,你完全正确。现在检查我的编辑。应该处理的缺点是它使用全局变量。好消息是
s=ccat(NULL,'A')也可以。如果您想处理更多的字符串,您应该为多个字符串及其长度使用一个结构。此外,您应该分配比现在需要更多的内存,并检查是否需要在
ccat
中重新分配它。如果您想要更高的性能,
realloc
ing不是一个便宜的操作。如果这是您的家庭作业,请尝试了解它是如何工作的以及为什么工作。缺点是它使用全局变量。好消息是
s=ccat(NULL,'A')也可以。如果您想处理更多的字符串,您应该为多个字符串及其长度使用一个结构。此外,您应该分配比现在需要更多的内存,并检查是否需要在
ccat
中重新分配它。如果您想要更高的性能,
realloc
ing不是一个便宜的操作。如果这是您的家庭作业,请尝试了解它是如何工作的以及为什么工作。