C 将3D数组(字符串的2D数组)传递给函数并在那里编辑它

C 将3D数组(字符串的2D数组)传递给函数并在那里编辑它,c,arrays,3d,C,Arrays,3d,我花了大量时间寻找这个答案,尽管我找到了一些类似的答案,但似乎没有什么对我有用,让我非常沮丧:/ 我想做的很简单。我想在一个函数中创建一个字符串的2D数组(字符串的2D数组是字符的3D数组),我想将它传递给另一个函数并在那里全局编辑它。如果一维可以是动态的,我会很高兴,但是我在malloc上遇到了很多麻烦,我甚至会接受一个选项,其中所有的三维都是静态的。只要我能将对它的引用传递给函数,在那个函数中填充它,然后在main中使用它。我正在寻找一个没有结构的解决方案 只要我通过数组[5][5][5]和

我花了大量时间寻找这个答案,尽管我找到了一些类似的答案,但似乎没有什么对我有用,让我非常沮丧:/

我想做的很简单。我想在一个函数中创建一个字符串的2D数组(字符串的2D数组是字符的3D数组),我想将它传递给另一个函数并在那里全局编辑它。如果一维可以是动态的,我会很高兴,但是我在malloc上遇到了很多麻烦,我甚至会接受一个选项,其中所有的三维都是静态的。只要我能将对它的引用传递给函数,在那个函数中填充它,然后在main中使用它。我正在寻找一个没有结构的解决方案

只要我通过数组[5][5][5]和void函数(char数组[5][5][5])简单地添加字符,我就成功地传递了字符的3d数组。但我不能这样添加字符串。我真的很赶时间。如果你不想回答我的问题,那就给我一个链接,链接到解释的地方,我会找到答案的

编辑:

#包括
#包括
#包括
void doit(字符***数组,整数x,整数y,整数z){
int i,j,k;
对于(i=0;i简单方法:

void foo(size_t height, size_t width, size_t length, char (*stringMatrix)[width][length]) {
    for(size_t y = 0; y < height; y++) {
        for(size_t x = 0; x < width; x++) {
            for(size_t i = 0; i < length; i++) {
                stringMatrix[y][x][i] = /*whatever*/;
            }
        }
    }
}

int main() {
    size_t height = 5, width = 7, length = 9;

    //use either of the following two lines
    char stringMatrix[height][width][length];
    //char (*stringMatrix)[width][length] = malloc(height * sizeof(*stringMatrix));

    foo(height, width, length, stringMatrix);

    //if you used malloc(), don't forget to free your matrix:
    //free(stringMatrix);
}
void foo(大小高度、大小宽度、大小长度、字符(*stringMatrix)[宽度][长度]){
用于(尺寸y=0;y<高度;y++){
用于(大小x=0;x
更好的方法是:

//Fills the matrix with malloc'd strings.
void foo(size_t height, size_t width, char* (*stringMatrix)[width]) {
    for(size_t y = 0; y < height; y++) {
        for(size_t x = 0; x < width; x++) {
            stringMatrix[y][x] = malloc(/* the length that you need for this string */);
            //initialize the string in stringMatrix[y][x]
        }
    }
}

int main() {
    size_t height = 5, width = 7;

    //use either of the following two lines
    char* stringMatrix[height][width];
    //char* (*stringMatrix)[width] = malloc(height * sizeof(*stringMatrix));

    foo(height, width, stringMatrix);

    //Cleanup
    for(size_t y = 0; y < height; y++) {
        for(size_t x = 0; x < width; x++) {
            free(stringMatrix[y][x]);
        }
    }
    //if you used malloc(), don't forget to free your matrix:
    //free(stringMatrix);
}
//用malloc'd字符串填充矩阵。
void foo(大小高度、大小宽度、字符*(*stringMatrix)[宽度]){
用于(尺寸y=0;y<高度;y++){
用于(大小x=0;x
这两种方法都使用真正的多维数组,该数组作为指向数组第一维第一个元素的指针传递给函数。区别在于,第一个版本使用真正的三维数组,而第二个版本使用的是一个二维数组
char*
,必须在函数中为每个字符串分配你可以填写矩阵

为什么第二个版本更好?仅仅是因为它没有对您可以处理的字符串的大小施加限制。在任何地方使用动态分配的字符串似乎都不方便,但事实是几乎每个固定长度的缓冲区都是一个等待发生的bug。迟早您会得到一个超过限制,你的程序就会爆炸


在POSIX-2008标准中,我们还可以使用多种字符串处理函数来生成现成的malloc'd字符串,如
strdup()
getline()
asprintf()
,等等。如果您使用这些函数,分配字符串以适应的许多麻烦就会消失。

展示您的成就。注意
void foo(char*array[5][5])
与void foo(char*(*array)[5])
相同:数组参数在调用和函数签名中衰减为指向其第一个元素的指针。即,如果使用
char*array[1][5];函数(数组)我的上帝,似乎有无限的方法来解决同样的问题。我还有很多事情要学习C.。谢谢大家的帮助,很抱歉占用你的时间。你最好学习C++。你现在可以在我的潜在困惑中做出选择。MALLC C党和CMASTER在堆栈上使用可变大小的数组,它可以为C99和更高的工作。在C++中,你可以有一个简单的标准的编译器支持的解决方案。如果你还需要编写一个程序,C++可以节省很多工作。C vs. C++是一个无休止的争论。在多维数组的区域,C规则。(我仍然使用C++的大部分时间,虽然:-)
void foo(size_t height, size_t width, size_t length, char (*stringMatrix)[width][length]) {
    for(size_t y = 0; y < height; y++) {
        for(size_t x = 0; x < width; x++) {
            for(size_t i = 0; i < length; i++) {
                stringMatrix[y][x][i] = /*whatever*/;
            }
        }
    }
}

int main() {
    size_t height = 5, width = 7, length = 9;

    //use either of the following two lines
    char stringMatrix[height][width][length];
    //char (*stringMatrix)[width][length] = malloc(height * sizeof(*stringMatrix));

    foo(height, width, length, stringMatrix);

    //if you used malloc(), don't forget to free your matrix:
    //free(stringMatrix);
}
//Fills the matrix with malloc'd strings.
void foo(size_t height, size_t width, char* (*stringMatrix)[width]) {
    for(size_t y = 0; y < height; y++) {
        for(size_t x = 0; x < width; x++) {
            stringMatrix[y][x] = malloc(/* the length that you need for this string */);
            //initialize the string in stringMatrix[y][x]
        }
    }
}

int main() {
    size_t height = 5, width = 7;

    //use either of the following two lines
    char* stringMatrix[height][width];
    //char* (*stringMatrix)[width] = malloc(height * sizeof(*stringMatrix));

    foo(height, width, stringMatrix);

    //Cleanup
    for(size_t y = 0; y < height; y++) {
        for(size_t x = 0; x < width; x++) {
            free(stringMatrix[y][x]);
        }
    }
    //if you used malloc(), don't forget to free your matrix:
    //free(stringMatrix);
}