如何Concat的两个char *用C? [英] How to concat two char * in C?
本文介绍了如何Concat的两个char *用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到一个char *缓冲区其中有10 lenght。
但我想在Concat的我的结构的全部内容其中有一个变量的char *。
I receive a char * buffer which have the lenght of 10. But I want to concat the whole content in my struct which have an variable char *.
typedef struct{
char *buffer;
//..
}file_entry;
file_entry real[128];
int fs_write(char *buffer, int size, int file) {
//every time this function is called buffer have 10 of lenght only
// I want to concat the whole text in my char* in my struct
}
事情是这样的:
real[i].buffer += buffer;
我怎样才能做到这在C?
How can I do this in C ?
推荐答案
在一般情况下,请执行下列操作(调整,并添加错误检查您认为合适的)
In general, do the following (adjust and add error checking as you see fit)
// real[i].buffer += buffer;
// Determine new size
int newSize = strlen(real[i].buffer) + strlen(buffer) + 1;
// Allocate new buffer
char * newBuffer = (char *)malloc(newSize);
// do the copy and concat
strcpy(newBuffer,real[i].buffer);
strcat(newBuffer,buffer); // or strncat
// release old buffer
free(real[i].buffer);
// store new pointer
real[i].buffer = newBuffer;
这篇关于如何Concat的两个char *用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文