小编Leo*_*Leo的帖子

使用指针在C中追加字符串

对于我的生活,我无法弄清楚为什么这个程序不起作用.我正在尝试使用指针连接两个字符串并继续收到此错误:

a.out(28095) malloc: *** error 
for object 0x101d36e9c: pointer being realloc'd was not allocated
*** set a breakpoint in malloc_error_break to debug
Run Code Online (Sandbox Code Playgroud)

我的str_append.c:

#include <stdio.h>
#include <stdlib.h>
#include "stringlibrary.h"  /* Include the header (not strictly necessary here) */

//appends s to d
void str_append(char *d, char *s){
  int i=0, j=0;

  d = realloc(d, strlength(d)+strlength(s)+1);
  //find the end of d
  while(*(d+i)!='\0'){
    i++;
  }


  //append s to d
  while(*(s+j)!='\0'){
    *(d+i)=*(s+j);
    i++;
    j++;
  }
  *(d+i)='\0';


}
Run Code Online (Sandbox Code Playgroud)

我有自己的strlength函数,我100%肯定有效.

我的主要内容:

#include <stdio.h>
#include <stdlib.h>
#include …
Run Code Online (Sandbox Code Playgroud)

c string pointers append

2
推荐指数
1
解决办法
4036
查看次数

标签 统计

append ×1

c ×1

pointers ×1

string ×1