C 将每个字符添加到数组中

Geo*_* Go 2 c

对于 C++ - 我可以将每个添加charstring数组中的每个索引:

   string *x=new string[10];
    x[0] += "a";
    x[0] += "b";
    x[0] += "c";
    x[1] += "x";
    x[1] += "y";
    x[1] += "z";
    cout << "x[0]=" << x[0] << endl; // would be "abc"
    cout << "x[1]=" << x[1] << endl; // would be "xyz"
Run Code Online (Sandbox Code Playgroud)

如何在 C 中执行相同的功能?我有一个buff2指向char数组的指针,并试图charbuf. 当我打印出buff2价值时,我不断得到奇怪的价值。

char buf[255];
char *buff2;
int i=0, count=0;
buff2=(char*)malloc(512*sizeof(char));

  while((n = read(fd, buf, sizeof(buf[g]))) > 0){
    for(i=0; i<n; i++){
      if(buf[i] == '\n'){
        l++;
        count2++;
      }
       else
     {   
       buff2[count2]+=buf[i];
     }
  }
Run Code Online (Sandbox Code Playgroud)

bru*_*uno 5

你的 C 代码有几个问题

  • buff是一个无用的数组,因为您只使用buff[0]
  • 变量l似乎从未定义/初始化过,而您却没有修改它
  • buff2[count2]+=buf[i];总是修改相同的buff2[count2]直到换行,因为在这种情况下你不会增加buff2而只有在读取换行时,你确定你想要吗?
  • 你没有以空字符结束buff2,这可能解释了当我打印出 buff2 值时我总是得到奇怪的值。
  • 你没有保护,以防你写出buff2产生未定义的行为

string *x=new string[10];
Run Code Online (Sandbox Code Playgroud)

可以在 C

char ** x = calloc(10, sizeof(char *));
Run Code Online (Sandbox Code Playgroud)

我使用calloc用空指针初始化

和相当于:

x[0] += "a";
Run Code Online (Sandbox Code Playgroud)

 strCat(&x[0], "a");
Run Code Online (Sandbox Code Playgroud)

和:

char * const strCat(char ** p, const char * s)
{
   if (s != NULL) {
     if (*p == NULL)
       *p = strdup(s);
     else { 
       size_t len = strlen(*p);

       *p = realloc(*p, len + strlen(s) + 1); /* may be detect realloc returns NULL on error */
       strcpy(*p + len, s);
     }
   }

   return *p;
}
Run Code Online (Sandbox Code Playgroud)

所以例如:

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

char * const strCat(char ** p, const char * s)
{
   if (s != NULL) {
     if (*p == NULL)
       *p = strdup(s);
     else { 
       size_t len = strlen(*p);

       *p = realloc(*p, len + strlen(s) + 1); /* may be detect realloc returns NULL on error */
       strcpy(*p + len, s);
     }
   }

   return *p;
}

int main()
{
  char ** x = calloc(10, sizeof(char *));

  strCat(&x[0], "a");
  strCat(&x[0], "b");
  strCat(&x[0], "c");
  
  strCat(&x[1], "x");
  strCat(&x[1], "y");
  strCat(&x[1], "z");
  
  printf("x[0]=%s\n", x[0]);
  printf("x[1]=%s\n", x[1]);
  
  free(x[0]);
  free(x[1]);
  free(x);
  
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译和执行:

% gcc -Wall a.c
% ./a.out
x[0]=abc
x[1]=xyz
%
Run Code Online (Sandbox Code Playgroud)

valgrind下运行:

% valgrind ./a.out
==113490== Memcheck, a memory error detector
==113490== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==113490== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==113490== Command: ./a.out
==113490== 
x[0]=abc
x[1]=xyz
==113490== 
==113490== HEAP SUMMARY:
==113490==     in use at exit: 0 bytes in 0 blocks
==113490==   total heap usage: 7 allocs, 7 frees, 98 bytes allocated
==113490== 
==113490== All heap blocks were freed -- no leaks are possible
==113490== 
==113490== For counts of detected and suppressed errors, rerun with: -v
==113490== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
%
Run Code Online (Sandbox Code Playgroud)

但是请注意,每次连接新字符串时,都需要遍历当前字符串以了解其长度,这不是由 std::string知道使用的长度的人以何种方式完成的,因为 KamilCuk 的回答就是这种情况