英文:
How can I concat two char pointers into one?
问题
I wanted to know how can I concat two char pointers and if it is even possible? I have a code here;
char* partOne = "1234"; // could have a known N number of bytes
char* partTwo = "5678"; // could have a known M number of bytes
char newPart[N+M];
for(int i= 0; i<N; i++){
newPart[i++] = *partOne++;
}
for(int i = N, j=0; j < M; j++){
newPart[i++] = *partTwo++;
}
std::cout << (char *) newPart << std::endl;
Could this be possible to achieve?
Edit:
to be clear on why using char pointers is due to UDP sockets. for example: data I received I have the length of the message in the header
while(!closed){
int bytesReceived = myApi.readData(); // N bytes
if(bytesReceived <= 0){
continue;
}
char *buffer = myApi.getBuffer(); // returns char * buffer
do{
if(startOf(buffer)){
//if true check for length of the message
int messagelen = getMessageLength(buffer);
while(bytesReeceived < messagelen){
char * partialBuffer;
int partialBytesReceived = myApi.readData(); //M bytes
if(partialBytesReceived <= 0) continue;
partialBuffer = myApi.getBuffer();
.....
//concat two pointers here
// update N bytesReceived, N += M;
}
}
} while(*buffer != '\0');
}
英文:
I wanted to know how can I concat two char pointers and if it is even possible? I have a code here;
char* partOne = "1234"; // could have a known N number of bytes
char* partTwo = "5678"; // could have a known M number of bytes
char newPart[N+M];
for(int i= 0; i<N; i++){
newPart[i++] = *partOne++;
}
for(int i = N, j=0; j < M; j++){
newPart[i++] = *partTwo++;
}
std::cout << (char *) newPart << std::endl;
Could this be possible to achieve?
Edit:
to be clear on why using char pointers is due to UDP sockets. for example: data I received I have the length of the message in the header
while(!closed){
int bytesReceived = myApi.readData(); // N bytes
if(bytesReceived <= 0){
continue;
}
char *buffer = myApi.getBuffer(); // returns char * buffer
do{
if(startOf(buffer)){
//if true check for length of the message
int messagelen = getMessageLength(buffer);
while(bytesReeceived < messagelen){
char * partialBuffer;
int partialBytesReceived = myApi.readData(); //M bytes
if(partialBytesReceived <= 0) continue;
partialBuffer = myApi.getBuffer();
.....
//concat two pointers here
// update N bytesReceived, N += M;
}
}
} while(*buffer != 'while(!closed){
int bytesReceived = myApi.readData(); // N bytes
if(bytesReceived <= 0){
continue;
}
char *buffer = myApi.getBuffer(); // returns char * buffer
do{
if(startOf(buffer)){
//if true check for length of the message
int messagelen = getMessageLength(buffer);
while(bytesReeceived < messagelen){
char * partialBuffer;
int partialBytesReceived = myApi.readData(); //M bytes
if(partialBytesReceived <= 0) continue;
partialBuffer = myApi.getBuffer();
.....
//concat two pointers here
// update N bytesReceived, N += M;
}
}
} while(*buffer != '\0');
}
');
}
In reality I only have access to the updated char * buffer every time I call it. I want to take into account partial messages coming from a UDP socket and bytes received
答案1
得分: 3
正确的方式是简单的:
int main(){
std::string partOne = "1234";
std::string partTwo = "5678";
std::string newPart = partOne + partTwo;
std::cout << newPart << std::endl;
}
英文:
The right way is the simple:
int main(){
std::string partOne = "1234";
std::string partTwo = "5678";
std::string newPart = partOne + partTwo;
std::cout << newPart << std::endl;
}
答案2
得分: 1
由于您正在使用C风格的字符串(字符数组),您可以使用strcpy
和strcat
将它们连接起来。
#include <iostream>
#include <cstring>
int main() {
char* partOne = "1234"; // 可能有已知的N个字节
char* partTwo = "5678"; // 可能有已知的M个字节
// 实际上,您应该使用`strlen`并在堆上分配内存
// 这样,您可以处理任何大小的缓冲区
char newPart[N+M+1];
// 复制第一个字符串
std::strcpy(newPart, partOne);
// 然后将第二个字符串附加到末尾
std::strcat(newPart, partTwo);
std::cout << newPart << '\n';
}
但是,您应该记住,在C++代码中使用C风格的字符串通常不是一个好主意。更安全的做法是从缓冲区创建std::string
对象,然后使用+
运算符将它们连接起来。这样做会减少缓冲区溢出和其他错误的风险。
英文:
Since you're using C-style strings (character arrays), you could join them by using strcpy
and strcat
.
#include <iostream>
#include <cstring>
int main() {
char* partOne = "1234"; // could have a known N number of bytes
char* partTwo = "5678"; // could have a known M number of bytes
// In reality you'd want to use `strlen` and allocate on the heap
// for this, so that you can deal with any size of buffer
char newPart[N+M+1];
// Copy the first string in
std::strcpy(newPart, partOne);
// Then add (concatenate) the second string to the end
std::strcat(newPart, partTwo);
std::cout << newPart << '\n';
}
However, you should keep in mind that using C-style strings in C++ code is often a bad idea. It would be safer to create std::string
objects from the buffers, and join them using the +
operator. Doing so would have less risk of buffer overruns and other bugs.
答案3
得分: 0
在你的情况下,你想要_避免_字符串拼接。 (你可以通过构造一个std::string
和一个std::string_view
来实现,使用起始指针和长度,然后使用+
操作符,但这是无意义的复制)。
相反,安排后续读取_追加_到您已经有的数据上。
这种情况的标准模式大致如下:
char buffer[BUFSIZE];
char *p = buffer;
std::size_t len = sizeof buffer;
while (/*需要更多数据*/) {
auto bytes_read = read(input_fd, buf, len);
if (bytes_read <= 0) {
/*错误处理在这里*/
return FAILURE_CODE;
}
p += bytes_read;
len -= bytes_read;
}
然后我们有p-buffer
字节存储在buffer
的开头。
英文:
In your case, you want to avoid string concatenation. (You could do that by constructing a std::string
and a std::string_view
, using start pointer and length for each, then using +
operator, but that's pointless copying).
Instead, arrange for the subsequent read to append to the data you already have.
The standard pattern for this goes something like:
char buffer[BUFSIZE];
char *p = buffer;
std::size_t len = sizeof buffer;
while (/*need more data*/) {
auto bytes_read = read(input_fd, buf, len);
if (bytes_read <= 0) {
/* error handling here */
return FAILURE_CODE;
}
p += bytes_read;
len -= bytes_read;
}
We then have p-buffer
bytes stored stored contiguously beginning at buffer
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论