C语言 多维数组中的字符串初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14089514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:55:06  来源:igfitidea点击:

String initialization in multidimensional array

cmultidimensional-array

提问by sabbir

How to assign string value in multidimensional array, send the array as a function argument, and return it back to main function? I tried this but it gives an error:

如何在多维数组中分配字符串值,将数组作为函数参数发送,并将其返回给主函数?我试过这个,但它给出了一个错误:

char a[250][250][250];   // does not work
a[][0][2] = "0";         // does not work
a[][1][2] = "0";         // does not work

char a[][2][2] = {"0", "1"};  // works

// error: expected primary-expression before ']' token
a[i][j][max] = add_func(a[i][j][], i, j); 

回答by Grijesh Chauhan

After declaration you can not assign, but use strcpy()

声明后,您不能分配,但可以使用 strcpy()

char a[250][250][250];

strcpy(a[0][1],"0");

or assign at the time of declaration:

或在声明时分配:

char a[250][250][250] = {"0","2"};  
char a[][250][250] = {"0","2"}; 

or if you want to assign a single char.

或者如果您想分配单个字符。

a[i][j][k] = '0'; 

Where i, j, k are any value less than 250

其中 i, j, k 是任何小于 250 的值



How to Declaration and Initialization 3D Array IN C

如何在 C 中声明和初始化 3D 数组

In-general a[3][4][2]is a three-dimension array, that can be view as

一般来说a[3][4][2]是一个三维数组,可以看作

a[3][4][2]: Consist of 3 two-dimension arrays, where each 2-D array are consist of 4-rows and 2-colunms. can be declare as:

a[3][4][2]:由3 个二维数组组成,其中每个二维数组由 4 行和 2 列组成。可以声明为:

char a[3][4][2] =  { 
                       { //oth 2-D array 
                         {"0"},
                         {"1"},
                         {"2"},
                         {"4"}
                       },
                       { //1th 2-D Array
                         {"0"},
                         {"1"},
                         {"2"},
                         {"4"}
                       },
                       { //2nd 2-D array
                         {"0"},
                         {"1"},
                         {"2"},
                         {"4"}
                       },
                   };  

Note: "1" means two chars, one additional fro null ('\0') char.

注意:“1”表示两个字符,另外一个来自 null ('\0') 字符。

If integer array:

如果整数数组:

int a[3][2][3]=  
        {
            { //oth 2-D array, consists of 2-rows and 3-cols
            {11, 12, 13},
            {17, 18, 19}
            },
            {//1th 2-D array, consists of 2-rows and 3-cols
            {21, 22, 23},
            {27, 28, 29}
            },
            {//2th 2-D array, consists of 2-rows and 3-cols
            {31, 32, 33},
            {37, 38, 39}
            },
        };

Link to understand

链接了解



Second error:

第二个错误

to this a[i][j][max]a char can assign not string so,

对此a[i][j][max],字符不能分配字符串,因此,

a[i][j][max] = '0' ; // is correct  expression 

but

a[i][j][max] = "0";  // is not correct, wrong   


Please read WhozCraig comment. you are declaring huge memory in stack!

请阅读WhozCraig 评论。您正在堆栈中声明巨大的内存!



According to your comment:

根据您的评论

function declaration:

函数声明:

char add_func(char a[250][250][250], int i, int j); // function definition  

try like this:

试试这样:

  char a[250][250][250];
  a[i][j][max] = add_func(a, i, j );

回答by Shiplu Mokaddim

To initializecharacter strings table you can use curly braces. And the outer most dimension (I dont know how else can it be called) or the left most number in square is optional.

初始化字符串表,您可以使用花括号。最外面的维度(我不知道还能怎么称呼)或正方形中最左边的数字是可选的。

So this will work

所以这会起作用

char table[][3][10] = {
    {"row1-col1", "row1-col2", "row1-col3"},
    {"row2-col1", "row2-col2", "row2-col3"},
    {"row3-col1", "row3-col2", "row3-col3"},
    {"row4-col1", "row4-col2", "row4-col3"}
    };

You dont need to type table[4][3][10]. Compiler calculates it. The size of tableis 120 bytes. As the contents are all string you can use

您不需要键入table[4][3][10]. 编译器计算它。的大小table为 120 字节。由于内容都是字符串,您可以使用

char *table[][3] = ...

This will save 20% space.

这将节省 20% 的空间

Curly braces can only be used in initializing phase. Not after that. hence following code will not work.

花括号只能在初始化阶段使用。不是在那之后。因此以下代码将不起作用。

a[][0][2] = "0"; 

回答by CubeSchrauber

You probably would like to use pointers instead:

您可能想改用指针:

char *a[2][2] = { "0", "1", "2", "3" };