C中的二维字符数组初始化

Der*_*rek 7 c arrays char multidimensional-array

我正在尝试构建一个字符串列表,我需要将其传递给期望的函数 char **

我该如何构建这个数组?我想传递两个选项,每个选项少于100个字符.

char **options[2][100];

options[0][0] = 'test1';
options[1][0] = 'test2';
Run Code Online (Sandbox Code Playgroud)

这不编译.我究竟做错了什么?如何在C中创建2D字符数组?

Pau*_*l R 25

C字符串用双引号括起来:

const char *options[2][100];

options[0][0] = "test1";
options[1][0] = "test2";
Run Code Online (Sandbox Code Playgroud)

重新阅读你的问题和评论虽然我猜你真正想做的是这样的:

const char *options[2] = { "test1", "test2" };
Run Code Online (Sandbox Code Playgroud)

  • 编辑您的问题,使其包含您尝试传递此数组的功能. (2认同)

Eri*_*ski 9

如何创建包含字符指针的数组大小5:

char *array_of_pointers[ 5 ];        //array size 5 containing pointers to char
char m = 'm';                        //character value holding the value 'm'
array_of_pointers[0] = &m;           //assign m ptr into the array position 0.
printf("%c", *array_of_pointers[0]); //get the value of the pointer to m
Run Code Online (Sandbox Code Playgroud)

如何创建指向字符数组的指针:

char (*pointer_to_array)[ 5 ];        //A pointer to an array containing 5 chars
char m = 'm';                         //character value holding the value 'm'
*pointer_to_array[0] = m;             //dereference array and put m in position 0
printf("%c", (*pointer_to_array)[0]); //dereference array and get position 0
Run Code Online (Sandbox Code Playgroud)

如何创建包含字符指针的2D数组:

char *array_of_pointers[5][2];          
//An array size 5 containing arrays size 2 containing pointers to char

char m = 'm';                           
//character value holding the value 'm'

array_of_pointers[4][1] = &m;           
//Get position 4 of array, then get position 1, then put m ptr in there.

printf("%c", *array_of_pointers[4][1]); 
//Get position 4 of array, then get position 1 and dereference it.
Run Code Online (Sandbox Code Playgroud)

如何创建指向2D数组字符的指针:

char (*pointer_to_array)[5][2];
//A pointer to an array size 5 each containing arrays size 2 which hold chars

char m = 'm';                            
//character value holding the value 'm'

(*pointer_to_array)[4][1] = m;           
//dereference array, Get position 4, get position 1, put m there.

printf("%c", (*pointer_to_array)[4][1]); 
//dereference array, Get position 4, get position 1
Run Code Online (Sandbox Code Playgroud)

为了帮助您了解人类应该如何阅读复杂的C/C++声明,请阅读:http://www.programmerinterview.com/index.php/c-cplusplus/c-declarations/