如何将两个char指针连接成一个?

huangapple go评论64阅读模式
英文:

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 = &quot;1234&quot;; // could have a known N number of bytes
    char* partTwo = &quot;5678&quot;; // could have a known M number of bytes

    char newPart[N+M];
    for(int i= 0; i&lt;N; i++){
       newPart[i++] = *partOne++;
    }
    for(int i = N, j=0; j &lt; M; j++){
       newPart[i++] = *partTwo++;
    }

    std::cout &lt;&lt; (char *) newPart &lt;&lt; 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 &lt;= 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 &lt; messagelen){
          char * partialBuffer;
          int partialBytesReceived = myApi.readData(); //M bytes
          if(partialBytesReceived &lt;= 0) continue;
          partialBuffer = myApi.getBuffer();
          .....
          //concat two pointers here

         // update N bytesReceived, N += M;
          
      }
    }
  } while(*buffer != &#39;
while(!closed){
int bytesReceived = myApi.readData(); // N bytes
if(bytesReceived &lt;= 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 &lt; messagelen){
char * partialBuffer;
int partialBytesReceived = myApi.readData(); //M bytes
if(partialBytesReceived &lt;= 0) continue;
partialBuffer = myApi.getBuffer();
.....
//concat two pointers here
// update N bytesReceived, N += M;
}
}
} while(*buffer != &#39;\0&#39;);
}
&#39;); }

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 = &quot;1234&quot;;
    std::string partTwo = &quot;5678&quot;;

    std::string newPart = partOne + partTwo;

    std::cout &lt;&lt; newPart &lt;&lt; std::endl;

}

答案2

得分: 1

由于您正在使用C风格的字符串(字符数组),您可以使用strcpystrcat将它们连接起来。

#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 &lt;iostream&gt;
#include &lt;cstring&gt;

int main() {
    char* partOne = &quot;1234&quot;; // could have a known N number of bytes
    char* partTwo = &quot;5678&quot;; // could have a known M number of bytes

    // In reality you&#39;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 &lt;&lt; newPart &lt;&lt; &#39;\n&#39;;
}

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 &lt;= 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 &lt;= 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.

huangapple
  • 本文由 发表于 2023年6月8日 21:58:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定