Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.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
C 创建二维动态字符串数组_C - Fatal编程技术网

C 创建二维动态字符串数组

C 创建二维动态字符串数组,c,C,有人能给我解释一下动态创建二维字符串数组的最简单方法吗?我有一个txt文件,里面有一些字符串,我想把这个txt文件传输到一个数组中。因此,我想将txt行号与第一个维度相关联,将字符串本身与第二个维度相关联。第二个维度是每行中的字符数(这是稳定的,因为txt中的每一行都有特定的语法),因此如果我的txt中有: hello how (newline) are youuu *(我写了youuu,因为正如我所说,每一行都有相同数量的字符) 我想要像这样的东西: array[0]["hello how"

有人能给我解释一下动态创建二维字符串数组的最简单方法吗?我有一个txt文件,里面有一些字符串,我想把这个txt文件传输到一个数组中。因此,我想将txt行号与第一个维度相关联,将字符串本身与第二个维度相关联。第二个维度是每行中的字符数(这是稳定的,因为txt中的每一行都有特定的语法),因此如果我的txt中有:

hello how (newline)
are youuu
*(我写了
youuu
,因为正如我所说,每一行都有相同数量的字符)

我想要像这样的东西:

array[0]["hello how"],
array[1]["are youuu"]

非数字键在C语言中是不允许的。你试图用一种只处理数字的语言来做一些PHP和JavaScript的废话

但是,C总是有两条通向地狱的路

char *lookup_key(int index, char *key) { ... }


printf(lookup_key(0, "hello how"));

如果您知道字符串的长度以及有多少个字符串,那么可以如下配置数组

char strings[numLines][strLen+1];
strcpy(strings[1], "test2");
然后可以像这样访问阵列

char strings[numLines][strLen+1];
strcpy(strings[1], "test2");
如果您事先不知道任何事情,则需要一个指向指针数组的指针,然后在数组增长时使用malloc分配空间,完成后释放空间。

C中的dynamic意味着您需要使用[C][m]alloc中的一个来为字符串创建内存。2D表示一个
char
数组的数组。假设您知道所需字符串的数量和最长字符串,下面将创建内存来包含它们:

char ** Create2DStr(ssize_t numStrings, ssize_t maxStrLen)
{
    int i;
    char **a = {0};
    a = calloc(numStrings, sizeof(char *));
    for(i=0;i<numStrings; i++)
    {
      a[i] = calloc(maxStrLen + 1, 1);
    }
    return a;
}

给出10个数组,每个数组可以包含20个字符,再加上一个NULL。(在
maxStrLen
之后的
+1
为空值提供了额外的空间)。

如果要将文件的每一行保存为数组中的一行,请使用
char
的二维数组:

char fileContents[NUM_LINES][LINE_LENGTH + 1]; // +1 for zero terminator
如果您不知道前面有多少行,则需要进行一些内存管理。首先,您需要分配一个初始范围:

#define INITIAL_EXTENT 20 // or some good starting point

char (*fileContents)[LINE_LENGTH + 1] = malloc( sizeof *fileContents * INITIAL_EXTENT );
if ( !fileContents )
{
  // malloc failed; fatal error
  fprintf( stderr, "FATAL: could not allocate memory for array\n" );
  exit( EXIT_FAILURE );
}
size_t numRows = INITIAL_EXTENT; // number of rows in array
size_t rowsRead = 0;             // number of rows containing data
在读取文件时,您将检查以确保阵列中有足够的空间;如果不这样做,则需要使用
realloc
调用来扩展阵列,这是一个潜在的昂贵操作。一种常见的技术是每次扩展时将数组的大小增加一倍,这将
realloc
调用的总数降至最低。如果由于只需再增加一行而将数组大小增加一倍,则可能会出现一些内部碎片,但这可能是您可以分析的:

char tmpBuf[LINE_LENGTH + 2]; // account for newline in input buffer

while ( fgets( tmpBuf, sizeof tmpBuf, inputFile ) )
{
  /**
   * Check to see if you have any room left in your array; if not, 
   * you'll need to extend it.  You'll probably want to factor this 
   * into its own function.
   */
  if ( rowsRead == numRows ) 
  {
    /**
     * Use a temporary variable for the result of realloc in case of failure
     */
    char (*tmp)[LINE_LENGTH + 1] = 
      realloc( fileContents, sizeof *fileContents * ( 2 * numRows ) );

    if ( !tmp )
    {
      /**
       * realloc failed - we couldn't extend the array any more.  
       * Break out of the loop. 
       */
      fprintf( stderr, "ERROR: could not extend fileContents array - breaking out of loop\n" );
      break;
    }
    /**
     * Otherwise, set fileContents to point to the new, extended buffer
     * and update the number of rows.  
     */
    fileContents = tmp;
    numRows *= 2;
  }

  // strip the newline from the input buffer
  char *newline = strchr( tmpBuf, '\n' );
  if ( newline )
    *newline = 0;

  strcpy( fileContents[rowsRead++], tmpBuf );
}

看看,你只是想知道如何动态分配一个二维数组,或者如何调整一个数组的大小?类似于,
char(*arr)[CHARS\u PER\u LINE]=malloc(sizeof(*arr)*NUM\u LINE)可能?在您的示例中,第一行有一个“(换行符)”,但第二行没有。“第二维度是字符数”和“字符串数组”有点矛盾。要在C中存储字符串,数组的大小必须为“字符数”+1。从技术上讲,这不是2D数组或数组数组数组。。。它是一个一维指针数组,每个指针指向一个单独分配的一维数组
char
。实际的2D数组也是可能的,但不同。那么
char(*arr)[21]=calloc(10,sizeof(*arr))?@Dmitri-是的,只要在运行时知道21就行了。我用过类似的东西。但如果没有,则需要malloc或calloc。当然,还有来自C99和更高版本的;字符(*字符串)[strLen+1]=malloc(sizeof*arr*numLines)