【问题标题】:Initializing 2D char array in C在 C 中初始化二维字符数组
【发布时间】:2014-11-15 01:23:27
【问题描述】:

类似这个问题:2d array, using calloc in C

我需要帮助初始化一个 2D 字符数组,该数组将全部初始化为某个值(在本例中为“0”)。我尝试了许多不同的方法,我正在拔头发。请让我知道我做错了什么。此代码不起作用。谢谢!

char** init_array() {
        char newarray[5][10];
        int i, j;
        for (i = 0; i < 5; i++) {
                for (j = 0; j < 10; j++) {
                        newarray[i][j] = '0';
                }
        }
        return newarray;
}
char **array = init_array();

我尝试编译时从 gcc 得到的错误:

test.c: In function ‘init_array’:
test.c:12:2: warning: return from incompatible pointer type [enabled by default]
  return newarray;
  ^
test.c:12:2: warning: function returns address of local variable [-Wreturn-local-addr]
test.c: At top level:
test.c:14:1: error: initializer element is not constant
 char **array = init_array();

应该这样吗?

char newarray[5][10];
char** init_array() {
        int i, j;
        for (i = 0; i < 5; i++) {
                for (j = 0; j < 10; j++) {
                        newarray[i][j] = '0';
                }
        }
        return newarray;
}
char **array = init_array();

【问题讨论】:

  • 不能返回本地数组,需要用malloc动态分配。
  • 或者将指针作为参数传递,然后您可以(1)在函数中对其进行操作(void类型行为)并(2)返回一个值(char**行为)
  • 从函数返回后,newarray 将超出范围。在 init_array 中分配内存。
  • char**char[][]相同。
  • 真的吗?我没有意识到这一点。我以为他们是一样的。

标签: c arrays initialization


【解决方案1】:

我认为图片有帮助。这是char newarray[5][10]。它是一个由 10 个字符组成的数组和其中 5 个字符组成的单个内存块。 您可以通过一个 memset 调用来清除它。

这里是char **array。它说array 是一个指针。 它指向什么? 指向字符的指针。

记住指针算法。 如果array是一个恰好指向一个指针的指针, 那么(*array)等于array[0],也就是array指向的指针。

array[1] 是什么? 它是array 指向的数组中的第二个指针

array[0][0] 是什么? 它是array指向的第一个指针指向的第一个字符。

array[i][j] 是什么? 这是array指向的第i指针的第j个字符。

那么newarrayarray 有什么关系呢?
简单的。 newarray[i][j]newarray 的第 i 个子数组的第 j 个字符。

所以从这个意义上说,它就像array,但没有下面的所有指针。

有什么区别? 好吧,array 的缺点是您必须逐个构建它。 OTOH,优点是您可以在构建时将其放大到想要的大小。 它不必生活在预先知道的固定大小内。

清如泥?

【讨论】:

  • 感谢您向我解释并让我了解 memset()。我是 C 的新手...
  • 您也可以在声明静态声明的数组时简单地将其归零:char newarray[5][10] = {{0}};
  • 那行不通。我刚试过。我希望就这么简单!
  • 等一下,你需要#define 不提供大小,就像创建一个可变长度数组一样。我会为你发布一个补充答案。
  • @mallux char newarray[5][10] = { 0 };
【解决方案2】:

根据我们在 cmets 中的讨论,这里有一个在声明时将数组值归零的快速示例。注意,值是#defined 作为常量:

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

#define MAXSTR 10
#define MAXLEN 1024

int main () {

    char myarray[MAXSTR][MAXLEN] = {{0}};  /* declare a static array of MAXSTR x MAXLEN */

    /* copy various strings to statically declared storage */
    strncpy (myarray[0],"  This is the first string", strlen("  This is the first string")+1);
    strncpy (myarray[1],"  This is the second string", strlen("  This is the second string")+1);
    strncpy (myarray[2],"  This is the third string", strlen("  This is the third string")+1);
    strncpy (myarray[3],"  This is the fourth string", strlen("  This is the fourth string")+1);
    strncpy (myarray[4],"  This is the fifth string", strlen("  This is the fifth string")+1);

    int i = 0;

    /* print the values */
    while (*myarray[i])
        printf ("  %s\n", myarray[i++]);

    return 0;
}

输出:

$ ./myar
    This is the first string
    This is the second string
    This is the third string
    This is the fourth string
    This is the fifth string

构建:

gcc -Wall -Wextra -o myar myarray.c

【讨论】:

  • 是的,你是对的。它会将它们“归零”,但实际上并未将它们设置为“0”。我以为你是这个意思。
  • 当然,您只是想要一个示例程序,例如将文件读入动态分配的数组或结构数组吗?
【解决方案3】:

为避免使用全局变量(如上面粘贴的第二个示例代码)并避免使用malloc,您可以在函数外部定义数组并将其传入,如下所示。您不需要返回任何内容,因为数组数据本身正在被修改。请注意,有必要在函数签名中定义数组的二级维度:

void init_array(char ary[][10]) {
    int i, j;
    for (i = 0; i < 5; i++) {
        for (j = 0; j < 10; j++) {
                ary[i][j] = '0';
        }
    }
}

int main(void)
{
    char newarray[5][10];
    init_array(newarray);
    printf("%c", newarray[1][1]); /* Testing the output */
    return 0;
}

返回“0”。

http://codepad.org/JbykYcyF

【讨论】:

  • 确保传递(或#define)大小,以免在函数中硬编码值:void init_array(char ary[][10], int rows)
猜你喜欢
  • 2014-05-14
  • 2011-09-29
  • 1970-01-01
  • 1970-01-01
  • 2011-06-04
  • 1970-01-01
  • 1970-01-01
  • 2012-09-27
  • 2016-11-12
相关资源
最近更新 更多