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

14

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

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

char **options[2][100];

options[0][0] = 'test1';
options[1][0] = 'test2';

这段代码无法编译。我到底做错了什么?如何在C中创建2D字符数组?


4
char **options[2][100] 是一个大小为 [2][100] 的指向字符指针的指针数组。 - sidyll
4个回答

35

C字符串被双引号包围:

const char *options[2][100];

options[0][0] = "test1";
options[1][0] = "test2";

重新阅读您的问题和评论,我猜测您实际想要做的是:

const char *options[2] = { "test1", "test2" };

这与larsmans的答案相同。然而,我的函数似乎仍在抱怨我没有传递char **。 - Derek
2
请编辑您的问题,包括您要传递此数组的函数。 - Paul R
如果我想为上述二维数组动态分配内存,我该怎么做呢..? @PaulR - ranaarjun
char (*str)[5] = malloc(sizeof(*str)*2); str[0][1] = "abcd"; 我是这样做的。但编译器报错了!!! - ranaarjun
@ranaarjun:你应该发布一个新问题,详细描述你的问题。 - Paul R
好的,保罗先生。 - ranaarjun

12

如何创建一个包含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

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

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

如何创建一个包含字符指针的二维数组:

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.

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

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

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


4
char **options[2][100];

声明了一个大小为100的指向指针的指针数组的大小为2的数组,指向的类型是char。您需要去掉一个*。您还需要将字符串文字放在双引号中。

我在将 char*[2][100] 传递给寻找 char** 的函数时,需要对其进行解引用或其他操作吗?目前,它正在抱怨 char*[2][100] 不是一个有效的参数。 - Derek
@Derek:你不能将“options”传递给这样的函数,但是你可以将options[i]传递给i{0,1}中。 - Fred Foo

1

我认为你最初想做的是只创建一个字符数组,而不是指针数组:

char options[2][100];

options[0][0]='t';
options[0][1]='e';
options[0][2]='s';
options[0][3]='t';
options[0][4]='1';
options[0][5]='\0';  /* NUL termination of C string */

/* A standard C library function which copies strings. */
strcpy(options[1], "test2");

上面的代码展示了两种不同的方法来设置你预留用于存储字符的内存中的字符值。

这也可以工作,因为一切都是字面的: char options[2][100] = {"test1", "test2"}; - dslake

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接