Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Memcpy覆盖三维数组中的区域_C_Multidimensional Array_Memcpy - Fatal编程技术网

Memcpy覆盖三维数组中的区域

Memcpy覆盖三维数组中的区域,c,multidimensional-array,memcpy,C,Multidimensional Array,Memcpy,我正在尝试做一个小bash,并将字符串解析为一个三维数组。第一个索引是cmd,第二个索引是参数,第三个索引是字符 不,我的问题是,当我记住最后一个参数时,它会将其写入下一个命令 int main() { char buffer[256] = {"c1 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 \n"}; char arg_list[10][10][255]; // fgets(buffer, 255, stdin); int c = 0; int ind

我正在尝试做一个小bash,并将字符串解析为一个三维数组。第一个索引是cmd,第二个索引是参数,第三个索引是字符

不,我的问题是,当我记住最后一个参数时,它会将其写入下一个命令

int main()
{
  char buffer[256] = {"c1 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 \n"}; 
  char arg_list[10][10][255];
//  fgets(buffer, 255, stdin);

  int c = 0;
  int index_cmd = 0;
  int index = 0;
  int index_arg = 1;
  int lastIndex = 0;
  int nxt_cmd = 0;

  for(c = 0; c < 255; c++)
  {
    if (buffer[c] == '\r' || buffer[c] == '\n' || buffer[c] == ' ')
    {
      if(index_cmd == 0 || nxt_cmd)
      {
        memcpy(arg_list[index_cmd][0], buffer + lastIndex, c - lastIndex);
        arg_list[index_cmd][0][c-lastIndex] = 0;
        index_cmd++;
        index_arg = 1;
        nxt_cmd = 0;
      }
      else if(buffer[c-1] == '&' || buffer[c-1] == '|')
      {
        memcpy(arg_list[index_cmd][0], buffer + lastIndex, c - lastIndex);
        arg_list[index_cmd][0][c-lastIndex] = 0;
        nxt_cmd = 1;
        index_cmd++;
        index_arg = 1;
      }
      else if(buffer[c-1] != ' ' && buffer[c+1] != ' ')
      {
        memcpy(arg_list[index_cmd-1][index_arg] , buffer + lastIndex, c - lastIndex);
        arg_list[index_cmd-1][index_arg][c-lastIndex] = 0;
        index_arg++;
      }
      lastIndex = c +1;
    }

  }

  int i,j,k = 0;
  for(i = 0; i < 4; i++)
  {
    for(j =1; j < 11; j++)
    {
        printf("command %d %s\n",i,arg_list[i][0]);
        printf("arg %d %s\n",j, arg_list[i][j]);
    }
  }
}
因此,它将argument10写入数组中的某个位置,如果提供了该位置,则下一个命令应该位于该位置

我真的不知道是什么问题,但我认为这与
memcpy
有关


非常感谢您的帮助:)

在C中,数组的索引是0…(数组中的元素数为-1)。那么关于:索引到

char arg_list[10][10][255];
第一个索引的范围为0…9

第二个索引的范围为0…9

第三个索引的范围为0…254


发布代码中索引超出允许范围的区域是错误的,并导致未定义的行为

在C中,数组的索引为0…(数组中的元素数-1)。那么关于:索引到

char arg_list[10][10][255];
第一个索引的范围为0…9

第二个索引的范围为0…9

第三个索引的范围为0…254


已发布代码中索引超出允许范围的区域是一个错误,并导致未定义的行为

请发布一个,包括所需的
#include
语句您希望得到什么输出?请清理代码,使其干净地编译。编译时,始终启用警告,然后修复这些警告。(对于
gcc
,至少使用:
-Wall-Wextra-Wconversion-pedantic-std=gnu11
)注意:其他编译器使用不同的选项来执行相同的操作以便于可读性和理解:1)请遵循公理:每行只有一条语句,并且(最多)每语句一个变量声明。2) 请始终缩进代码。注2:使用可变宽度字体时,空间缩进级别可能/将丢失。建议每个缩进级别为4个空格。3) 请分开代码块:
对于
如果
其他
执行…当
切换
默认值
通过一个空行4)在括号内、括号内、大括号内、分号后、逗号后插入适当的空格,关于C运算符,限制变量的范围是一种很好的编程实践。例如:关于代码< >(C=0;C<255;C++)<代码>:(代码为<代码(0);C<255;C++)< /代码>其他的循环存在类似的考虑。请张贴A,包括需要的<代码>包含<代码>语句。您希望输出什么?请清理代码,以便它进行干净编译。编译时,始终启用警告,然后修复这些警告。(对于
gcc
,至少使用:
-Wall-Wextra-Wconversion-pedantic-std=gnu11
)注意:其他编译器使用不同的选项来执行相同的操作以便于可读性和理解:1)请遵循公理:每行只有一条语句,并且(最多)每语句一个变量声明。2) 请始终缩进代码。注2:使用可变宽度字体时,空间缩进级别可能/将丢失。建议每个缩进级别为4个空格。3) 请分开代码块:
对于
如果
其他
执行…当
切换
默认值
通过一个空行4)在括号内、括号内、大括号内、分号后、逗号后插入适当的空格,关于C运算符,限制变量的范围是一种很好的编程实践。例如:关于<代码>(C=0;C<255;C++)< /COD> >将其写成:<代码>(SiZeZT C=0;C<255;C++)< /代码>对于其他循环存在类似的考虑。非常感谢您的良好编程提示和答案!非常感谢您的优秀编程提示和答案!:)